0

我构建了一个非常基本的 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

然而,当我访问那个 URL 时,我看到了这个

{"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 数据......

4

1 回答 1

3

你忘了http.

$fullUrl = "http://localhost/serverList/api/rest.php?action=allenvironments&format=json";
于 2013-07-10T07:49:02.113 回答