繁体   English   中英

从 CURL PHP 请求获取响应 0

[英]Getting response 0 from CURL PHP request

我正在尝试使用代理访问页面,但是当我尝试不使用代理时,页面会给出 200 响应并获取数据,如果我尝试使用代理,我会得到 0 响应但没有数据。 我尝试了许多在网上找到的不同代理。 你能看出我做错了什么吗?

header( 'Cache-Control: max-age=60' );

$url = "https://hidester.com/what-is-my-ip-address";
echo "<br/>Search URL: ".$url."<br/><br/>";

// Get page
get_page($url);

var_dump(get_page($url, "193.202.8.57:8085"));


function get_page($url,$proxy = NULL)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    //curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_HEADER, 0); // return headers 0 no 1 yes
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return page 1:yes
    curl_setopt($ch, CURLOPT_TIMEOUT, 200); // http request timeout 20 seconds
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects, need this if the url changes

    if($proxy !== null)
    {
        echo "<br/> Trying proxy: {$proxy}";
        // no need to specify PROXYPORT again
        curl_setopt($ch, CURLOPT_PROXY, $proxy);

        // to make the request go through as though proxy didn't exist
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
    }

    curl_setopt($ch, CURLOPT_MAXREDIRS, 2); //if http server gives redirection responce
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); // cookies storage / here the changes have been made
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
    curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // the page encoding

    $data = curl_exec($ch); // execute the http request
    $info = curl_getinfo($ch);

    echo "<br/>Response: {$info["http_code"]} URL: {$info["url"]}<br/>";

    curl_close($ch); // close the connection
    return $data;
}

如果您稍微调整您的功能以包含port它似乎可以正常工作

function get_page( $url, $proxy = false, $port=false ){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 200);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    if( !empty( $proxy ) && !empty( $port ) ){
        curl_setopt($ch, CURLOPT_PROXY, $proxy );
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0 );
        curl_setopt($ch, CURLOPT_PROXYPORT, $port );
    }
    curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_ENCODING, "gzip");

    $data = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    return $data;
}

使用您提供的相同网址,但使用不同的代理和端口

$url='https://hidester.com/what-is-my-ip-address/';
$proxy='137.74.144.21';
$port=8080;

并像这样调用:

$res=get_page( $url, $proxy, $port );
printf('<pre>%s</pre>',print_r($res,true));

使用不同的curl函数以及详细输出的附加选项可能会帮助您诊断问题是否出在 GoDaddy 托管上。 如果您下载cacert.pem的副本并编辑下面的路径,您应该会获得有关请求的更多详细信息。

<?php

    function curl( $url=NULL, $options=NULL, $headers=false ){
        $cacert='c:/wwwroot/cacert.pem';
        $vbh = fopen('php://temp', 'w+');

        /* get cacert.pem from here */
        /* https://curl.haxx.se/docs/caextract.html */
        $res=array(
            'response'  =>  NULL,
            'info'      =>  array( 'http_code' => 100 ),
            'headers'   =>  NULL,
            'errors'    =>  NULL
        );
        if( is_null( $url ) ) return (object)$res;

        session_write_close();

        $curl=curl_init();
        if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
            curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
            curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
            curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
        }
        curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
        curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $curl, CURLOPT_FAILONERROR, true );
        curl_setopt( $curl, CURLOPT_HEADER, false );
        curl_setopt( $curl, CURLINFO_HEADER_OUT, false );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $curl, CURLOPT_BINARYTRANSFER, true );
        curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 20 );
        curl_setopt( $curl, CURLOPT_TIMEOUT, 60 );
        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, CURLOPT_MAXREDIRS, 10 );
        curl_setopt( $curl, CURLOPT_ENCODING, '' );
        curl_setopt( $curl, CURLOPT_VERBOSE, true );
        curl_setopt( $curl, CURLOPT_NOPROGRESS, true );
        curl_setopt( $curl, CURLOPT_STDERR, $vbh );

        if( isset( $options ) && is_array( $options ) ){
            foreach( $options as $param => $value ) curl_setopt( $curl, $param, $value );
        }
        if( $headers && is_array( $headers ) ){
            curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
        }
        $res=(object)array(
            'response'  =>  curl_exec( $curl ),
            'info'      =>  (object)curl_getinfo( $curl ),
            'errors'    =>  curl_error( $curl )
        );
        rewind( $vbh );
        $res->verbose=stream_get_contents( $vbh );
        fclose( $vbh );
        curl_close( $curl );
        return $res;
    }





    $url='https://hidester.com/what-is-my-ip-address/';
    $url='https://www.whatismyip.com/';
    $url='https://api.ipify.org?format=json';

    $proxy='137.74.144.21';
    $port=8080;



    $options=array(
        CURLOPT_PROXY           =>  $proxy,
        CURLOPT_PROXYPORT       =>  $port,
        CURLOPT_HTTPPROXYTUNNEL =>  true
    );

    $res=curl( $url, $options );
    if( $res->info->http_code==200 ){
        printf('<pre>%s</pre>',print_r($res->response,true));
        printf('<pre>%s</pre>',print_r($res->verbose,true));
        printf('<pre>%s</pre>',print_r($res->info,true));
    }

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM