Php 为什么我不能从API解码我的响应?
我试图从这个来源获得数据,这是我国学校的详细情况。但是当我得到响应时,我无法解码我的响应,它说Php 为什么我不能从API解码我的响应?,php,laravel,api,guzzle,guzzle6,Php,Laravel,Api,Guzzle,Guzzle6,我试图从这个来源获得数据,这是我国学校的详细情况。但是当我得到响应时,我无法解码我的响应,它说Null。Idk为什么,但是当我尝试复制并粘贴我的响应结果到硬代码时,它可以被解码。为什么? 我已经尝试了所有可能的方法来解决这个问题,没有任何效果 这是我的代码: $client = new \GuzzleHttp\Client(); $res = $client->request('GET', 'http://jendela.data.kemdikbud.go.id/api/index.ph
Null
。Idk为什么,但是当我尝试复制并粘贴我的响应结果到硬代码时,它可以被解码。为什么?
我已经尝试了所有可能的方法来解决这个问题,没有任何效果
这是我的代码:
$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方法:
$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或如果编码的数据
比递归限制更深
您可以使用或函数来确定问题
我无法获得整个响应,它已超时并已终止。根据文档:
如果无法解码json或编码数据深度超过递归限制,则返回NULL
资料来源:
一些调试提示:
utf8
json\u last\u error
参见:如果调试提示1恰好是问题的原因,请查看是否可以将返回限制为不包含有问题的字符。删除行
return$response代码>
并尝试以下代码:
$enc = mb_detect_encoding($response);
if($enc == 'UTF-8') {
$response = preg_replace('/[^(\x20-\x7F)]*/','', $response);
}
echo "<pre>";
print_r(json_decode($response,true));
$enc=mb\u detect\u编码($response);
如果($enc==“UTF-8”){
$response=preg_replace('/[^(\x20-\x7F)]*/',''$response);
}
回声“;
打印(json解码($response,true));
我尝试了json\u last\u错误,但它返回4
,我认为是语法错误。我该如何解决这个问题?@BobbyRinaldy尝试缩短响应时间,或增加php时间限制。@adam很遗憾,由于政府API的原因,我无法修复json。@shingo你能试着运行curl吗?我已经把它添加到我的问题中了。回答仍然被终止。你能试着运行curl方法吗?我已经把它添加到我的问题中了。我测试了它,它成功了!这对我来说很有效,我想知道我们为什么要这么做?