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

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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器