Php 我在响应cURL请求时获取空数据
我正在使用api执行一个cURL请求来获取信息,但当我使用json_decode时,它没有提供任何信息,而是返回空值。请通过这些线-Php 我在响应cURL请求时获取空数据,php,jquery,curl,Php,Jquery,Curl,我正在使用api执行一个cURL请求来获取信息,但当我使用json_decode时,它没有提供任何信息,而是返回空值。请通过这些线- // Initiate curl $ch = curl_init(); // Disable SSL verification curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Will return the response, if false it print the response curl_s
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,"http://mkp.gem.gov.in/oem-cartridge/samsung-111s-toner-rst/p-5116877-68482402616-cat.html");
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
这是使用API发出cURL请求和获取信息的正确方法,还是建议我如何做,因为我不熟悉这个主题。下面的代码行
$result=curl_exec($ch);
返回301永久移动
因为您的URL是仅使用HTTP的
http://mkp.gem.gov.in/oem-cartridge/samsung-111s-toner-rst/p-5116877-68482402616-cat.htm
但该站点在HTTPS上运行,服务器设置为强制/重定向此仅HTTP的URL到HTTPS,即。
https://mkp.gem.gov.in/oem-cartridge/samsung-111s-toner-rst/p-5116877-68482402616-cat.html
您可以将url更改为https,也可以使用
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirect if any
但它仍然呈现HTML而不是JSON
但是在你的评论中,你说这个URL正在与node一起工作,在这种情况下,请交叉检查你的URL,或者尝试使用POSTMAN发出相同的请求,看看显示了什么,因为该URL不是API端点,也不是返回JSON。我对node js使用相同的API,它产生了数据。这怎么可能?