douqinlu4217 2016-11-16 15:34
浏览 39

从php api返回json

I am new to programming and would like to begin adding more advanced applications to my site. I am trying to call an api with php. I'm having trouble getting it to return json format.

<?php


if(!empty($_GET['hospital_name'])) {
$Hospcomp_url = 'https://data.medicare.gov/resource/rbry-mqwu.json?hospital_name=' . urlencode($_GET['hospital_name']);

$Hospcomp_json = file_get_contents($Hospcomp_url);
json_decode($Hospcomp_json, true);
}

?>


<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CMS</title>
</head>
<body>
<form action="">

<input type="text" name="hospital_name"><br>
<input type="submit" value="Submit">

</form>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • dongzhiyong8577 2016-11-16 15:43
    关注

    It seems, you get your result properly but you don't do anything with it. First of all, you don't assign the array decoded from JSON to a variable. And then, you don't echo or process your array otherwise.
    Please replace this:

    json_decode($Hospcomp_json, true);
    

    with something like this:

    $decoded = json_decode($Hospcomp_json, true);
    var_export($decoded);
    

    Then you'll have your output and you will be able to decide what to do next.

    评论
  • duan33360 2016-11-16 15:45
    关注

    Calls are correct, just var_export it.

    if(!empty($_GET['hospital_name'])) {
        $Hospcomp_url = 'https://data.medicare.gov/resource/rbry-mqwu.json?hospital_name=' . urlencode($_GET['hospital_name']);
    
        $Hospcomp_json = file_get_contents($Hospcomp_url);
        var_export(json_decode($Hospcomp_json, true));
    }
    
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部