php - 为什么我无法对 API 的响应进行 json_decode?

标签 php laravel api guzzle guzzle6

我尝试从此来源获取数据,这是我所在国家/地区的学校详细信息。但是当我收到响应时,我无法解码我的响应,它说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_errorjson_last_error_msg函数来确定问题。

我无法获得完整的响应,超时并终止。

关于php - 为什么我无法对 API 的响应进行 json_decode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53878780/

相关文章:

php - laravel uuid 未在查询中显示

php - Laravel get() 与()

php - Laravel 存储纬度和经度

rest - 部署带有依赖项的 REST API

javascript - Twitter 搜索 API created_at 添加时间 Javascript

php - Mongo-PHP - MongoDB PHP 驱动程序 v1.6 的 MongoCursor 异常

PHP 查询返回 null !不可能

php - Laravel 所有 session ID 与 Redis 驱动程序

php - 导入时,二进制 IP 地址上的 API 调用显示奇怪的格式

php - 隐藏 WooCommerce 感谢页面订单详细信息