doubingjiu3199 2018-05-29 02:27
浏览 276

我收到NULL数据以响应cURL请求

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.

  • 写回答

1条回答 默认 最新

  • dongre1907 2018-05-29 10:16
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算