所以,我正在寻找一个使用php的网页标题。在研究了5秒钟后,我在这里找到了答案:
function get_title($url){
$str = file_get_contents($url);
if(strlen($str)>0){
$str = trim(preg_replace('/\s+/', ' ', $str));
preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title);
return $title[1];
}
}但我需要通过Tor代理,所以5秒钟研究这个网站和你的智慧,我发现:
$aContext = array(
'http' => array(
'proxy' => 'proxy:port',
'request_fulluri' => true,
)
);
$cxContext = stream_context_create($aContext);把所有这些放在一起,我这样做:
$aContext = array(
'http' => array(
'proxy' => '127.0.0.1:9150',
'request_fulluri' => true,
)
);
$cxContext = stream_context_create($aContext);
function get_title($url){
global $cxContext;
$str = file_get_contents($url, False, $cxContext);
if(strlen($str)>0){
$str = trim(preg_replace('/\s+/', ' ', $str));
preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title);
return $title[1];
}
}
echo get_title('http://' . $theonionurl);但是,这是行不通的。日志显示:
PHP警告: file_get_contents(http://the_onion_address_to_check.onion):无法打开流:在第44行的/var/www/html/mychecker.php中拒绝连接,引用:http://my_onion_address.onion/mychecker.php
我将端口更改为9050,但仍然无法工作。
我做错了什么?
(显然,我检查了,要检查的urls是有效的,并且可以通过浏览器访问)
发布于 2018-06-17 12:38:29
Tor是否已在您的系统上安装并运行?连接被拒绝将表明该端口上没有侦听任何内容。
您首先需要安装并运行Tor,然后才能使用它连接到站点。
此外,端口9050是一个SOCKS代理,而不是HTTP代理,因此您不能将其与HTTP stream proxy上下文选项一起使用,因为这只能与HTTP代理一起使用。
相反,如果你想使用Tor,你应该使用curl及其代理选项:
$ch = curl_init('http://example.onion/');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5_HOSTNAME,
CURLOPT_PROXY => '127.0.0.1:9050',
CURLOPT_HEADER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_ENCODING => '',
CURLOPT_COOKIEFILE => '',
]);
$response = curl_exec($ch);
if ($response === false) {
echo sprintf(
"Request failed. Error (%d) - %s\n",
curl_errno($ch),
curl_error($ch)
);
exit;
}
if (preg_match('/<title>(.*)<\/title>', $response, $match)) {
echo "The title is '{$match[1]}'";
} else {
echo "Did not find title in page."
}发布于 2018-06-16 02:38:52
您的$aContext在函数之外。
在函数内部移动它,它应该可以工作。
function get_title($url){
$aContext = array(
'http' => array(
'proxy' => '127.0.0.1:9150',
'request_fulluri' => true,
)
);
$cxContext = stream_context_create($aContext);
$str = file_get_contents($url, False, $cxContext);
if(strlen($str)>0){
$str = trim(preg_replace('/\s+/', ' ', $str));
preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title);
return $title[1];
}
}
echo get_title('http://' . $theonionurl);对全局的事情不太确定。
我从未使用过它,而且我发现使用局部变量更安全。
https://stackoverflow.com/questions/50880938
复制相似问题