简体   繁体   中英

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. 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.

Your following line of code

$result=curl_exec($ch);

returning 301 Moved Permanently

since your URL is use HTTP only 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

You can either change your url to https or set follow redirection true using

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

but still it renders HTML not 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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