wesley tanaka

Connecting through tor from php

‹ Tor Central Directory | Dream ›
()

The tor process exposes a SOCKS proxy. SOCKS is a pretty simple protocol, so it's pretty easy to create a TCP connection (HTTP or otherwise) through tor from php. This code isn't a generic SOCKS client -- it relies heavily on the way that tor happens to behave.

$domain = 'en.wikipedia.org';
$port = 80;
$path = '/';
$socket = fsockopen('localhost', 9050);

if ($socket)
{
fwrite($socket, "\x05\x01\x00");
fread($socket, 2);
fwrite($socket, "\x05\x01\x00\x03".chr(strlen($domain)));
fwrite($socket, $domain);
fwrite($socket, pack('n', $port));
fread($socket, 10);
fwrite($socket, 'GET '.$path
. " HTTP/1.0\r\n"
. "Host: $domain\r\n"
. "Connection: Close\r\n\r\n");
$string = '';
while ($socket && !feof($socket))
{
$string .= fread($socket, 4098);
}
fclose($socket);

echo $string;

echo "\n";
}
Syndicate content