所以,我正在寻找一个使用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-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
复制相似问题