Skip to content

Instantly share code, notes, and snippets.

@zofe
Last active August 30, 2016 04:30
Show Gist options
  • Save zofe/7469419 to your computer and use it in GitHub Desktop.
Save zofe/7469419 to your computer and use it in GitHub Desktop.
grab page with polipo and tor
<?php
function grab_page($url, $data=array())
{
$agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1';
$referer = "http://www.bing.com";
$url = str_replace(' ', '%20', $url);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_PROXY, "http://127.0.0.1:8123/");
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
curl_setopt($curl, CURLOPT_REFERER, $referer);
if (count($data)>0){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
$output = curl_exec($curl);
if (curl_errno($curl))
{
trigger_error('CURL error: "' . curl_error($curl) . '"', E_USER_WARNING);
$output = false;
}
curl_close($curl);
return $output;
}
function tor_new_identity($tor_ip='127.0.0.1', $control_port='9051', $auth_code='')
{
$fp = fsockopen($tor_ip, $control_port, $errno, $errstr, 30);
if (!$fp) return false; //can't connect to the control port
fputs($fp, "AUTHENTICATE $auth_code\r\n");
$response = fread($fp, 1024);
list($code, $text) = explode(' ', $response, 2);
if ($code != '250') return false; //authentication failed
//send the request to for new identity
fputs($fp, "signal NEWNYM\r\n");
$response = fread($fp, 1024);
list($code, $text) = explode(' ', $response, 2);
if ($code != '250') return false; //signal failed
fclose($fp);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment