我尝试从此来源获取数据,这是我所在国家/地区的学校详细信息。但是当我收到响应时,我无法解码我的响应,它说Null
。我不知道为什么,但是当我尝试将响应结果复制并粘贴到硬编码时,它可以被解码。为什么?
我已经尝试了所有可能的方法来解决这个问题,但没有任何效果。
这是我的代码:
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', 'http://jendela.data.kemdikbud.go.id/api/index.php/Csekolah/detailSekolahGET?mst_kode_wilayah=026700');
$response = $res->getBody()->getContents();
$result = json_decode($response); // this return NULL
//But when i going to return the $response, it show the response.
return $response;
我希望访问数据或者只是解码我的代码,这会对我有很大帮助。
仅供引用,我正在使用 Guzzle 6 和 Laravel 5.7 来处理这个问题。
我希望有人也可以尝试访问它并帮助我。
或者如果你想测试它,你可以使用 Curl Ways :
$param = 'index.php/Csekolah/detailSekolahGET?mst_kode_wilayah=026700';
$url='http://jendela.data.kemdikbud.go.id/api/'.$param;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
return $response;
最佳答案
json_decode文件说:
NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
您可以使用json_last_error或json_last_error_msg函数来确定问题。
我无法获得完整的响应,超时并终止。
关于php - 为什么我无法对 API 的响应进行 json_decode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53878780/