当URL包含数据时,不返回json数据

时间:2013-07-10 07:47:51

标签: php json api

我构建了一个非常基本的webAPI,在调用时会在屏幕上打印一些json数据。

我用以下

来呼叫api
function getEnvironmentList(){
    $fullUrl = "localhost/serverList/api/rest.php?action=allenvironments&format=json";
    $jsonDataRaw = file_get_contents($fullUrl);
    return $jsonDataRaw;
}
$jsonData = getEnvironmentList();
echo "<PRE>";
var_dump(json_decode($jsonData, true));
echo "</PRE>";

我收到错误Warning: file_get_contents(localhost/serverList/api/rest.php?action=allenvironments&format=json): failed to open stream: No error in C:\path\inc\getJSONdata.php on line 6

然而,当我访问该网址时,我看到了这个

{"1":{"environmentID":"1","envName":"UAT","envCreatedBy":"mhopkins","envCreatedDtTm":"2013-06-30 00:34:57","envUpdatedBy":"mhopkins","envUpdatedDtTm":"2013-06-30 00:34:57"},"3":{"environmentID":"3","envName":"Platinum","envCreatedBy":"mhopkins","envCreatedDtTm":"2013-06-30 00:37:38","envUpdatedBy":"phpsense","envUpdatedDtTm":"2013-06-30 00:37:38"}}

我真的很困惑为什么代码似乎无法意识到那里有json数据......

1 个答案:

答案 0 :(得分:3)

您忘记了http

$fullUrl = "http://localhost/serverList/api/rest.php?action=allenvironments&format=json";