curl_setopt 504网关超时

时间:2017-08-10 05:49:48

标签: php curl

我将代码发送请求的代码写入代理服务器的IP地址。但是这段代码给出了错误

  

504网关超时

我也尝试增加php.ini中的超时。但这也行不通。这是我试图使用的代码

<?php

$curl = curl_init();

$timeout = 300;

$proxies = file("proxy.txt");

$r="https://www.youtube.com/watch?v=iglQXfPXJHE";

//$r ="https://www.youtube.com/watch?v=rcWMxmKbj7c";

// Not more than 2 at a time

for($x=0;$x<2000; $x++){

//setting time limit to zero will ensure the script doesn't get timed out

set_time_limit(300);



//now we will separate proxy address from the port

//$PROXY_URL=$proxies[$getrand[$x]];

echo $proxies[$x];

curl_setopt($curl, CURLOPT_URL,$r);

curl_setopt($curl , CURLOPT_PROXY ,  preg_replace('/\s+/', '',$proxies[$x]));

curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");

curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);

curl_setopt($curl, CURLOPT_REFERER, "http://google.com/");

$text = curl_exec($curl);



echo "Hit Generated:";

echo htmlentities($x);



}



?>

感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:0)

给定一个文本文件,使用以下内容调用proxy.txt

198.110.57.6    8080    US  United States   anonymous   no  yes 1 minute ago
35.193.215.131  8080    US  United States   anonymous   no  yes 1 minute ago
198.50.219.239  80  CA  Canada  anonymous   no  yes 1 minute ago
217.61.124.144  80  IT  Italy   anonymous   no  yes 1 minute ago
171.255.199.5   80  VN  Vietnam anonymous   no  no  1 minute ago

以下PHP代码

define('ROOT','c:/wwwroot');

function curlproxy( $url, $ip, $port, $https ){
    $cacert=ROOT . '/cacert.pem';
    $curl=curl_init();
    if( $https==true ){
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, 0 );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt( $curl, CURLOPT_CAINFO, realpath( $cacert ) );
        $proxy='https://'.$ip .':' . $port;
    } else {
        $proxy='http://'.$ip .':' . $port;
    }
    $vbh = fopen('php://temp', 'w+');

    curl_setopt( $curl, CURLOPT_URL, $url );
    curl_setopt( $curl, CURLOPT_AUTOREFERER, TRUE );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE );
    curl_setopt( $curl, CURLOPT_FRESH_CONNECT, TRUE );
    curl_setopt( $curl, CURLOPT_FORBID_REUSE, TRUE );
    curl_setopt( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
    curl_setopt( $curl, CURLOPT_CLOSEPOLICY, CURLCLOSEPOLICY_OLDEST );
    curl_setopt( $curl, CURLOPT_MAXCONNECTS, 1 );
    curl_setopt( $curl, CURLOPT_FAILONERROR, TRUE );

    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE );
    curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 20 );
    curl_setopt( $curl, CURLOPT_TIMEOUT, 20 );
    curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' );
    curl_setopt( $curl, CURLINFO_HEADER_OUT, FALSE );
    curl_setopt( $curl, CURLOPT_NOBODY, TRUE );

    curl_setopt( $curl, CURLOPT_PROXY, $proxy );
    curl_setopt( $curl, CURLOPT_HTTPPROXYTUNNEL, TRUE );
    curl_setopt( $curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );

    curl_setopt( $curl, CURLOPT_VERBOSE, TRUE );
    curl_setopt( $curl, CURLOPT_NOPROGRESS, TRUE );
    curl_setopt( $curl, CURLOPT_STDERR, $vbh );

    $payload=(object)array_filter( array(
        'response'  =>  curl_exec( $curl ),
        'info'      =>  (object)curl_getinfo( $curl ),
        'errors'    =>  curl_error( $curl ),
        'request'   =>  array(
                'url'   =>  $url,
                'ip'    =>  $ip,
                'port'  =>  $port,
                'https' =>  $https,
                'proxy' =>  $proxy
            )
        ) 
    );
    curl_close( $curl );

    rewind( $vbh );
    $payload->verbose=stream_get_contents( $vbh );
    fclose( $vbh );

    return $payload;
}


$data=array();
$url='https://www.youtube.com/watch?v=iglQXfPXJHE';
$list = file('c:/temp/proxy.txt');

foreach( $list as $i => $line ){
    list($ip,$port,$code,$country,$anomynous,$google,$https,$up)=explode(chr(9),$line);
    $data[]=curlproxy( $url, $ip, $port, $https );
}


echo '<pre>',print_r($data,true),'</pre>';

为从free-proxy-list.net随机选择的某些代理提供了合理的结果,其中显示了一个小代码段

Array
(
    [0] => stdClass Object
        (
            [info] => stdClass Object
                (
                    [url] => https://lightspeed.ravennaschools.org/access?YT91X4Q5J1HNFABCRE8BZ5FZ22WS4KQ2
                    [content_type] => text/html
                    [http_code] => 200
                    [header_size] => 721
                    [request_size] => 970
                    [filetime] => -1
                    [ssl_verify_result] => 19
                    [redirect_count] => 1
                    [total_time] => 1.763
                    [namelookup_time] => 0
                    [connect_time] => 0.14
                    [pretransfer_time] => 0.624
                    [size_upload] => 0
                    [size_download] => 0
                    [speed_download] => 0
                    [speed_upload] => 0
                    [download_content_length] => 0
                    [upload_content_length] => 0
                    [starttransfer_time] => 0.764
                    [redirect_time] => 0.999
                    [certinfo] => Array
                        (
                        )

                )

            [request] => Array
                (
                    [url] => https://www.youtube.com/watch?v=iglQXfPXJHE
                    [ip] => 198.110.57.6
                    [port] => 8080
                    [https] => yes
                    [proxy] => https://198.110.57.6:8080
                )

            [verbose] => * About to connect() to proxy 198.110.57.6 port 8080 (#0)
*   Trying 198.110.57.6... * connected
* Connected to 198.110.57.6 (198.110.57.6) port 8080 (#0)
* Establish HTTP proxy tunnel to www.youtube.com:443
> CONNECT www.youtube.com:443 HTTP/1.1
Host: www.youtube.com:443
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Proxy-Connection: Keep-Alive

< HTTP/1.1 200 Connection established
< 
* Proxy replied OK to CONNECT request
* successfully set certificate verify locations:
*   CAfile: C:\wwwroot\cacert.pem
  CApath: none
* SSL connection using AES256-SHA
* Server certificate:
*    subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.google.com
*    start date: 2017-07-25 08:46:44 GMT
*    expire date: 2017-10-17 08:28:00 GMT
*    subjectAltName: www.youtube.com matched
*    issuer: C=US; ST=California; L=Bakersfield; O=Lightspeed Systems; OU=Support; CN=Lightspeed Rocket; emailAddress=support@lightspeedsystems.com
*    SSL certificate verify result: self signed certificate in certificate chain (19), continuing anyway.
> HEAD /watch?v=iglQXfPXJHE HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Host: www.youtube.com
Accept: */*

< HTTP/1.1 302 Moved Temporarily
< Server: squid/3.3.13
< Date: Thu, 10 Aug 2017 06:52:52 GMT
< Content-Length: 0
< Location: https://lightspeed.ravennaschools.org/access?YT91X4Q5J1HNFABCRE8BZ5FZ22WS4KQ2
< X-Cache: MISS from lightspeed.ravennaschools.org
< Via: 1.1 lightspeed.ravennaschools.org (squid/3.3.13)
< Connection: close
< 
* Closing connection #0
* Issue another request to this URL: 'https://lightspeed.ravennaschools.org/access?YT91X4Q5J1HNFABCRE8BZ5FZ22WS4KQ2'
* About to connect() to proxy 198.110.57.6 port 8080 (#0)
*   Trying 198.110.57.6... * connected
* Connected to 198.110.57.6 (198.110.57.6) port 8080 (#0)
* Establish HTTP proxy tunnel to lightspeed.ravennaschools.org:443
> CONNECT lightspeed.ravennaschools.org:443 HTTP/1.1
Host: lightspeed.ravennaschools.org:443
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Proxy-Connection: Keep-Alive

< HTTP/1.1 200 Connection established
< 
* Proxy replied OK to CONNECT request
* successfully set certificate verify locations:
*   CAfile: C:\wwwroot\cacert.pem
  CApath: none
* SSL connection using AES256-SHA
* Server certificate:
*    subject: OU=Domain Control Validated; CN=lightspeed.ravennaschools.org
*    start date: 2017-08-01 16:01:01 GMT
*    expire date: 2020-08-01 16:01:01 GMT
*    subjectAltName: lightspeed.ravennaschools.org matched
*    issuer: C=US; ST=California; L=Bakersfield; O=Lightspeed Systems; OU=Support; CN=Lightspeed Rocket; emailAddress=support@lightspeedsystems.com
*    SSL certificate verify result: self signed certificate in certificate chain (19), continuing anyway.
> HEAD /access?YT91X4Q5J1HNFABCRE8BZ5FZ22WS4KQ2 HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Host: lightspeed.ravennaschools.org
Accept: */*
Referer: https://www.youtube.com/watch?v=iglQXfPXJHE

< HTTP/1.1 200 OK
< Server: nginx/1.10.0
< Date: Thu, 10 Aug 2017 06:52:53 GMT
< Content-Type: text/html
< Expires: Thu, 10 Aug 2017 06:52:52 GMT
< Cache-Control: no-cache
< Cache-Control: no-cache
< Pragma: no-cache
< X-UA-Compatible: IE=Edge,chrome=1
< X-Lightspeed: suite
< X-Cache: MISS from lightspeed.ravennaschools.org
< Via: 1.1 lightspeed.ravennaschools.org (squid/3.3.13)
< Connection: keep-alive
* no chunk, no close, no size. Assume close to signal end
< 
* Closing connection #0

        )

但是,如果此脚本的唯一目的是增加点击计数器,那么您可能需要重新考虑,因为这似乎不会受到影响,但也许上述内容将会有用。