编程语言
首页 > 编程语言> > PHP Curl Tor页面未显示

PHP Curl Tor页面未显示

作者:互联网

我有这个代码

<?php 

$ua = array(
"Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7; AOLBuild 4343.19; Windows NT 6.1; WOW64; Trident/5.0; FunWebProducts)",
"Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; XH; rv:8.578.498) fr, Gecko/20121021 Camino/8.723+ (Firefox compatible)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1",
"Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko",
"Mozilla/5.0 (X11; U; Linux i686; fr-fr) AppleWebKit/525.1+ (KHTML, like Gecko, Safari/525.1+) midori/1.19",
"Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16",
"Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");

$uar = array_rand($ua);

$url = "sometestserverisetup";  
$ip = '127.0.0.1';
$port = '9051';
$auth = 'mypwwhateveritis';
$command = 'signal NEWNYM';

$fp = fsockopen($ip,$port,$error_number,$err_string,10);
if(!$fp) { echo "ERROR: $error_number : $err_string";
return false;
} else {
fwrite($fp,"AUTHENTICATE \"".$auth."\"\n");
$received = fread($fp,512);
fwrite($fp,$command."\n");
$received = fread($fp,512);
}

fclose($fp);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9050");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch,CURLOPT_USERAGENT,$ua[$uar]);
$response = curl_exec($ch);

echo $response;
?>

一切正常.与我的测试站点并正确显示.但是,某些网站(google.com,amazon.com,youtube,facebook.)仅显示空白页面以进行回声响应.

是否需要启用某些卷曲设置才能使页面正确显示.

解决方法:

看一个var_dump(curl_getinfo($ch));调用curl_exec之后可能会有所帮助.

我测试了您的代码,发现在某些情况下,网站发送带有Location标头的302移动响应,以重定向浏览器,这将在成功请求后导致空响应.

新增中

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

做到这一点,以便您提到的每个站点在我的测试中始终返回响应.并且根据您的工作(搜索,登录,表单提交),您可能会发现重定向很常见,因此您需要告诉cURL使用该选项来跟随它们.

除此之外,您可以将CURLOPT_HEADER设置为true,以便您查看发送的响应标头,以了解curl_getinfo之外的情况,以确保连接成功(通过Tor或到站点).

标签:tor,curl,html,php
来源: https://codeday.me/bug/20191119/2037957.html