我正在尝试使用代理访问页面,但是当我尝试不使用代理时,页面给出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;
}
答案 0 :(得分:0)
如果您稍微调整一下功能以包含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));
}
?>