感谢draw010!你在哪里对,我没有通过身份验证。现在是我了。
但是,使用下面的代码,每次我运行此脚本时,它都会使用不同的 ip 获取 http://www.watismijnip.nl。
但它需要我们一个新的 ip,在每次获取页面之后。现在不是这样了。
我意识到端口 9051 需要一个新的身份,而 curl 使用端口 9050。但是如果我让 curl 使用端口 9051,它会返回并返回空字符串。
这样可以吗?
function tor_new_identity($tor_ip='127.0.0.1', $control_port='9051', $auth_code=''){
$fp = fsockopen($tor_ip, $control_port, $errno, $errstr, 30);
if (!$fp) return false; //can't connect to the control port
fputs($fp, "AUTHENTICATE $auth_code\r\n");
$response = fread($fp, 1024);
echo 'debug: $response='.$response."\n<br>";
list($code, $text) = explode(' ', $response, 2);
if ($code != '250') return false; //authentication failed
//send the request to for new identity
fputs($fp, "signal NEWNYM\r\n");
$response = fread($fp, 1024);
list($code, $text) = explode(' ', $response, 2);
if ($code != '250') return false; //signal failed
fclose($fp);
return true;
}
if (tor_new_identity('127.0.0.1', '9051', '"my_password"')) {
echo "Identity switched!";
}else{
echo "Identity NOT switched!";
}
$url='http://www.watismijnip.nl/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9050");
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0');
$output = curl_exec($ch);
$curl_error = curl_error($ch);
curl_close($ch);
print_r($output);
print_r($curl_error);
if (tor_new_identity('127.0.0.1', '9051', '"my_password"')) {
echo "Identity switched!";
}else{
echo "Identity NOT switched!";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9050");
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0');
$output = curl_exec($ch);
$curl_error = curl_error($ch);
curl_close($ch);
print_r($output);
print_r($curl_error);