phplinuxfile-get-contentstor

file_get_contents through tor


So, I was looking for away to find a title of a page using php. After researching for 5 seconds, I found right here, the answer:

        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];
        }
      }

But I need that through Tor Proxy, so 5 seconds research on this sites and your wisdom, I found:

        $aContext = array(
        'http' => array(
            'proxy' => 'proxy:port',
            'request_fulluri' => true,
        )
    );

    $cxContext = stream_context_create($aContext);

Puting it all together, I did this:

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

But, that's not working. The log shows:

PHP Warning: file_get_contents(http://the_onion_address_to_check.onion): failed to open stream: Connection refused in /var/www/html/mychecker.php on line 44, referer: http://my_onion_address.onion/mychecker.php

I changed the port to 9050, still not working.

What am I doing wrong ???

(obviously, I checked, the urls to check are valid and accessible through tor browser)


Solution

  • Is Tor installed and running on your system? Connection refused would indicate that nothing is listening on that port.

    You first need to install and run Tor before you can use it to connect to sites.

    Also, port 9050 is a SOCKS proxy, not an HTTP proxy, therefore you won't be able to use it with the HTTP stream proxy context option as this only works with HTTP proxies.

    Instead, you should use curl along with its proxy options if you want to use 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."
    }