简体   繁体   English

我正在响应url请求而获取NULL数据

[英]I am getting NULL data in response to cURL request

I am doing a cURL request to fetch information using api but when I use json_decode, it's not giving any information rather it's returning NULL value. 我正在执行cURL请求以使用api来获取信息,但是当我使用json_decode时,它没有提供任何信息,而是返回NULL值。 Please go through these lines- 请通过这些行-

//  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));

Is this correct way to make cURL request and fetch information using API, or suggest me how to do as I am new to this topic. 这是使用API​​进行cURL请求和获取信息的正确方法,还是建议我该如何做,因为我是本主题的新手。

Your following line of code 您的以下代码行

$result=curl_exec($ch);

returning 301 Moved Permanently 返回301永久移动

since your URL is use HTTP only http://mkp.gem.gov.in/oem-cartridge/samsung-111s-toner-rst/p-5116877-68482402616-cat.htm 由于您的网址仅使用HTTP http://mkp.gem.gov.in/oem-cartridge/samsung-111s-toner-rst/p-5116877-68482402616-cat.htm

but this site runs on HTTPS, and server is setup to force/redirect this HTTP only URL to HTTPS ie https://mkp.gem.gov.in/oem-cartridge/samsung-111s-toner-rst/p-5116877-68482402616-cat.html 但是此站点在HTTPS上运行,并且服务器已设置为强制/仅将此HTTP URL重定向到HTTPS,即https://mkp.gem.gov.in/oem-cartridge/samsung-111s-toner-rst/p-5116877-68482402616-cat.html

You can either change your url to https or set follow redirection true using 您可以将网址更改为https,也可以使用将true重定向设为true

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirect if any

but still it renders HTML not JSON. 但仍然呈现HTML而不是JSON。

But in your comment you says this same URL is working with node, in such case please cross check your URL or try to make same request using POSTMAN and see what is shows 但是在您的评论中,您说此相同的URL正在与节点一起使用,在这种情况下,请交叉检查您的URL或尝试使用POSTMAN发出相同的请求,然后查看显示的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM