Page 1 of 1

Using a proxy URL

Posted: Fri Mar 16, 2012 4:27 am
by Mince
Hi All

I need to send data from an intranet through a proxy url. I have the following code:

Code: Select all

<?php
function proxy_url($proxy_url)
{
	$error_no = 0;
	$error_string = "";
   $proxy_name = 'proxy.co.za';
   $proxy_port = 80;
   $proxy_user = "username";
   $proxy_pass = "password";
   $proxy_cont = '';
   $proxy_fp = fsockopen($proxy_name, $proxy_port,$error_no,$error_string);
   if (!$proxy_fp)    {return false;}
   fputs($proxy_fp, "GET $proxy_url HTTP/1.0\r\nHost: $proxy_name\r\n");
   fputs($proxy_fp, "Proxy-Authorization: Basic " . base64_encode ("$proxy_user:$proxy_pass") . "\r\n\r\n");
   while(!feof($proxy_fp)) {$proxy_cont .= fread($proxy_fp,4096);}
   fclose($proxy_fp);
   $proxy_cont = substr($proxy_cont, strpos($proxy_cont,"\r\n\r\n")+4);
   echo "ERROR: " . $error_no . $error_string . "<br>";
   return $proxy_cont;
}

$a = proxy_url("destination_ip:8080/httppost_test/recieve.php");
var_dump($a);
?>
When i run this code I get:

Image

But when i try the URL directly in the browser's address bar, it works fine. Any suggestions?

Thanks
Marinus

Re: Using a proxy URL

Posted: Fri Mar 16, 2012 6:16 am
by requinix
Proxy requests look like

Code: Select all

GET http://www.example.com/path HTTP/1.0
Host: www.example.com
- The request URI is the full thing including protocol
- The Host: is the hostname for the requested resource, not for the proxy itself

Or you could use cURL instead. It takes care of absolutely everything for you so you don't have to skip bothering to learn how these kinds of HTTP requests and responses actually work.

Re: Using a proxy URL

Posted: Mon Mar 19, 2012 3:20 am
by Mince
I've seen lots of people suggesting cURL. Thanks, I'll look into that.