PHP并行cURL 502坏网关

时间:2018-03-04 14:38:53

标签: php curl

我使用并行cURL通过URL下载网页源代码比单个卷曲更快,但有时我得到随机量的502错误。如果我使用单个cURL而不是并行,我会得到更少的502.任何解决方案我该如何解决这个问题?

502 Bad Gateway
The server returned an invalid or incomplete response

经过测试:

  1. Localhost - XAMPP - PHP版本7.0.13

  2. VPS - (LAMP) - PHP版本7.0.25

  3. PHP

        $curl_arr = array();
        $master = curl_multi_init();
    
        for($i = 0; $i < count($urls); $i++)
        {
            $random = rand(1,9999);
    
            $link = trim($urls[$i]);
    
            $link = $link."?".$random;
    
            $url = trim($link);
            $curl_arr[$i] = curl_init($url);
            curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl_arr[$i], CURLOPT_FRESH_CONNECT, TRUE);
            curl_setopt($curl_arr[$i], CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36');
            curl_setopt($curl_arr[$i], CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl_arr[$i], CURLOPT_ENCODING , "gzip");
            curl_setopt($curl_arr[$i], CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
    
            curl_multi_add_handle($master, $curl_arr[$i]);
        }
        do 
        {
            curl_multi_exec($master,$running);
        } 
        while($running > 0);
    

0 个答案:

没有答案