Php 通过tor获取文件内容

Php 通过tor获取文件内容,php,linux,file-get-contents,tor,Php,Linux,File Get Contents,Tor,所以,我一直在寻找使用php的页面标题。经过5秒钟的研究,我在这里找到了答案: function get_title($url){ $str = file_get_contents($url); if(strlen($str)>0){ $str = trim(preg_replace('/\s+/', ' ', $str)); preg_match("/\<title\>(.*)\<

所以,我一直在寻找使用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];
        }
      }
综合起来,我做到了:

        $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);
$aContext=数组(
“http'=>数组(
'代理'=>'127.0.0.1:9150',
'request_fulluri'=>true,
)
);
$cxContext=stream\u context\u create($aContext);
函数get_title($url){
全球环境;
$str=file\u get\u contents($url,False,$cxContext);
如果(strlen($str)>0){
$str=修剪(预替换('/\s+/','$str));
预匹配(“/\(.*)\/i”,$str,$title);
返回$title[1];
}
}
echo get_title('http://.$theonionur);
但是,这是行不通的。日志显示:

PHP警告:file_get_contents():无法打开流:第44行/var/www/html/mychecker.PHP中的连接被拒绝,请参考:

我将端口更改为9050,仍然无法工作

我做错了什么


(显然,我检查过了,要检查的URL是有效的,可以通过tor浏览器访问)

您的
$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);
函数获取标题($url){
$aContext=数组(
“http'=>数组(
'代理'=>'127.0.0.1:9150',
'request_fulluri'=>true,
)
);
$cxContext=stream\u context\u create($aContext);
$str=file\u get\u contents($url,False,$cxContext);
如果(strlen($str)>0){
$str=修剪(预替换('/\s+/','$str));
预匹配(“/\(.*)\/i”,$str,$title);
返回$title[1];
}
}
echo get_title('http://.$theonionur);
不确定全球的事情。

我从未使用过它,我发现使用局部变量更安全。

您的系统上是否安装并运行了Tor?拒绝连接将表明该端口上没有任何内容正在侦听

您首先需要安装并运行Tor,然后才能使用它连接到站点

此外,端口9050是一个
SOCKS
代理,而不是HTTP代理,因此您将无法将其与HTTP流代理上下文选项一起使用,因为这仅适用于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."
}
$ch=curl\u init('http://example.onion/');
curl_setopt_数组($ch[
CURLOPT_RETURNTRANSFER=>1,
CURLOPT_PROXYTYPE=>CURLPROXY_SOCKS5_主机名,
CURLOPT_PROXY=>“127.0.0.1:9050”,
CURLOPT_头=>0,
CURLOPT_FOLLOWLOCATION=>1,
CURLOPT_编码=>'',
CURLOPT_COOKIEFILE=>“”,
]);
$response=curl\u exec($ch);
如果($response==false){
埃科斯普林特(
“请求失败。错误(%d)-%s\n”,
旋度(克),
旋度误差($ch)
);
出口
}
if(preg_match('/(.*),$response,$match)){
echo“标题为“{$match[1]}”;
}否则{
echo“在页面中找不到标题。”
}

不!“PHP警告:文件\u get\u contents():无法打开流:第109行/var/www/html/mychecker.PHP中的连接被拒绝,请参考:”仍然。谢谢!卷发起作用了![必须先安装]@Borna默认端口仍然是9050。9150由Tor浏览器包使用。
$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."
}