Skip to content

Instantly share code, notes, and snippets.

@memphys
Created April 16, 2011 18:05
Show Gist options
  • Save memphys/923354 to your computer and use it in GitHub Desktop.
Save memphys/923354 to your computer and use it in GitHub Desktop.
fetch some url through tor
<?php
class Fetcher
{
private $_torIp = '127.0.0.1';
private $_torProxyPort = '8118';
protected function _request($url)
{
$res = curl_init();
curl_setopt_array($res, array(
CURLOPT_PROXY => $this->_torIp . ':' . $this->_torProxyPort,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
));
$response = curl_exec($res);
if (curl_getinfo($res, CURLINFO_HTTP_CODE) != '200') {
return false;
}
curl_close($res);
return $response;
}
}
<?php
class Tor
{
private static $_torIp;
private static $_torPort;
public static function newIdentity()
{
$fp = fsockopen(self::$_torIp, self::$_torPort, $errno, $errstr,
if (! $fp) {
return false;
}
fputs($fp, "AUTHENTICATE\r\n");
$response = fread($fp, 1024);
list($code, $text) = explode(' ', $response, 2);
if ($code != '250') {
return false;
}
fputs($fp, "signal NEWNYM\r\n");
$response = fread($fp, 1024);
list($code, $text) = explode(' ', $response, 2);
if ($code != '250') {
return false;
}
fclose($fp);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment