Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a proxy server with fopen

I'm trying to use fopen to read a remote file from another website. I want to use a proxy to do this, and as far as I know I can do:

$context = stream_context_create(array(
    'http' => array(
        'proxy' => 'tcp://192.168.10.10:80' // The proxy server address and port
    ),
));

$file = fopen($url, 'r', false, $context)

but is there a way to authenticate using a username and password for that proxy? Or am I limited to having to use public proxies?

Also, the proxy definition is tcp://192.168.10.10:80. If I wanted to use a HTTP proxy, am I free to just change it to http://192.168.10.10:80?

Thanks.

like image 254
James Dawson Avatar asked Nov 04 '22 03:11

James Dawson


1 Answers

You can use the Proxy-Authorization in the $context if the proxy requires authentication.Example from PHP.Net:

$opts= array(
                 'http' => array( 
                 'proxy' => 'tcp://proxyip:8080', 
                 'header' => array( 
                                     "Proxy-Authorization: Basic $auth" 
                                  ) 
                ) 
); 
$context = stream_context_create($opts); 

Also,I think you cannot change tcp to http in tcp://192.168.10.10:80 since what the proxy does is establishing a TCP connection to another server on behalf of a client then route all the traffic back and forth between the client and the server.Obviously it concerns with TCP(sometimes UDP) instead of HTTP.Http proxies just "understand" the traffic delivered by HTTP protocol.

like image 152
Young Avatar answered Nov 10 '22 12:11

Young