douqi1625 2016-05-06 11:05
浏览 144
已采纳

CURLINFO_HTTP_CODE在PHP中输出错误状态代码的问题

I'm experiencing a strange issue with CURLINFO_HTTP_CODE in PHP where it is returning a 0 even though the cURL request itself has been successful. Essentially I'm using a for loop to perform changes to base bids for every campaign ID. I'm then trying to catch cases where the HTTP status code of the request is not equal to 200 and display these to the browser.

The issue is that return var_dump($this->info) returns int(0) which doesn't make sense if the request has been succesful. I've checked this by making a GET request to the campaign ID. The code I'm using is below:

class curl
  {
     public $info;
     public $json_output;

     public function curlPut($url, $JSON, $token)
     {
        $ch = curl_init($url);
        $popt = array(
           CURLOPT_CUSTOMREQUEST => 'PUT',
           CURLOPT_RETURNTRANSFER => TRUE,
           CURLOPT_SSL_VERIFYPEER => FALSE,
           CURLOPT_POSTFIELDS => $JSON,
           CURLOPT_HTTPHEADER => array(
              'Content-Type: application/json',
              'Authorization:'.$token.''
           ));
        curl_setopt_array($ch, $popt);
        $this->info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $this->json_output = curl_exec($ch);
        curl_close($ch);
        return var_dump($this->info);
     }
  };

Does anyone know why my script is showing this behaviour?

Any comments would be appreciated!

Thanks,

Sam

  • 写回答

1条回答 默认 最新

  • donglie9067 2016-05-06 11:09
    关注

    You're trying to get the status code before you've executed the request.

    Instead, swap:

    $this->json_output = curl_exec($ch);
    $this->info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题