【问题标题】:file_get_contents through tor通过 tor 的 file_get_contents
【发布时间】:2018-11-25 14:46:32
【问题描述】:

所以,我一直在寻找使用 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,还是不行。

我做错了什么???

(显然,我检查了,要检查的 url 是有效的,并且可以通过 Tor 浏览器访问)

【问题讨论】:

    标签: php linux file-get-contents tor


    【解决方案1】:

    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."
    }
    

    【讨论】:

    • 谢谢!卷曲正在工作! [必须先安装]
    • @Borna 默认端口仍然是 9050。9150 被 Tor Browser Bundle 使用。
    【解决方案2】:

    您的$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);
    

    不确定那个全球性的事情。
    我从未使用过它,我发现使用局部变量更安全。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2017-03-12
    • 1970-01-01
    • 2012-08-09
    • 2019-06-04
    • 1970-01-01
    相关资源
    最近更新 更多