php curl_exec returns empty

前端 未结 4 923
刺人心
刺人心 2020-12-16 15:08
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_PROXY, $proxy); // $proxy is ip of          


        
相关标签:
4条回答
  • 2020-12-16 15:43

    curl_exec will return false when the request failed. Adjust your function to this :

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXY, $proxy); // $proxy is ip of proxy server
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    
    $httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
    $response = curl_exec($ch);
    
    if ($response === false) 
        $response = curl_error($ch);
    
    echo stripslashes($response);
    
    curl_close($ch);
    

    This way u'll see the curl error

    0 讨论(0)
  • 2020-12-16 15:48

    You're trying to access a HTTP response code before you actually make the HTTP call. Reverse the execution as follows:

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
    
    0 讨论(0)
  • 2020-12-16 16:06

    The result return 0 mean that you can not connect to the server so please recheck your proxy and increase the timeout :)

    0 讨论(0)
  • 2020-12-16 16:09

    Try:

    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
    

    Maybe the response is longer than 10.

    I had the same issue, I solved like this.

    0 讨论(0)
提交回复
热议问题