Using a proxy URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mince
Forum Commoner
Posts: 25
Joined: Mon Aug 03, 2009 9:36 am

Using a proxy URL

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using a proxy URL

Post 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.
Mince
Forum Commoner
Posts: 25
Joined: Mon Aug 03, 2009 9:36 am

Re: Using a proxy URL

Post by Mince »

I've seen lots of people suggesting cURL. Thanks, I'll look into that.
Post Reply