PHP cURL HTTP CODE return 0

前端 未结 9 1951
青春惊慌失措
青春惊慌失措 2020-11-29 23:03

I dont understand when I echo $httpCode I always get 0, I was expecting 404 when I change $html_brand into a broken url. Is there anything that I miss or do not know of? Tha

9条回答
  •  暖寄归人
    2020-11-29 23:28

    Another reason for PHP to return http code 0 is timeout. In my case, I had the following configuration:

    curl_setopt($http, CURLOPT_TIMEOUT_MS,500);

    It turned out that the request to the endpoint I was pointing to always took more than 500 ms, always timing out and always returning http code 0.

    If you remove this setting (CURLOPT_TIMEOUT_MS) or put a higher value (in my case 5000), you'll get the actual http code, in my case a 200 (as expected).

    See https://www.php.net/manual/en/function.curl-setopt.php

提交回复
热议问题