Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents behind a proxy?

Tags:

php

proxy

At work we have to use a proxy to basically access port 80 for example, we have our own custom logins for each user.

My temporary workaround is using curl to basically login as myself through a proxy and access the external data I need.

Is there some sort of advanced php setting I can set so that internally whenever it tries to invoke something like file_get_contents() it always goes through a proxy? I'm on Windows ATM so it'd be a pain to recompile if that's the only way.

The reason my workaround is temporary is because I need a solution that's generic and works for multiple users instead of using one user's credentials ( Ive considered requesting a separate user account solely to do this but passwords change often and this technique needs to be deployed throughout a dozen or more sites ). I don't want to hard-code credentials basically to use the curl workaround.

like image 364
meder omuraliev Avatar asked Aug 26 '09 17:08

meder omuraliev


People also ask

Which is faster cURL or file_get_contents?

This is old topic but on my last test on one my API, cURL is faster and more stable. Sometimes file_get_contents on larger request need over 5 seconds when cURL need only from 1.4 to 1.9 seconds what is double faster.

Is file_get_contents secure?

file_get_contents in itself appears safe, as it retrieves the URL and places it into a string. As long as you're not processing the string in any script engine or using is as any execution parameter you should be safe.

What is file_get_contents PHP input?

file_get_contents() function: This function in PHP is used to read a file into a string. json_decode() function: This function takes a JSON string and converts it into a PHP variable that may be an array or an object.

Does file_get_contents cache?

Short answer: No. file_get_contents is basically just a shortcut for fopen, fread, fclose etc - so I imagine opening a file pointer and freading it isn't cached.


1 Answers

To use file_get_contents() over/through a proxy that doesn't require authentication, something like this should do :

(I'm not able to test this one : my proxy requires an authentication)

$aContext = array(     'http' => array(         'proxy'           => 'tcp://192.168.0.2:3128',         'request_fulluri' => true,     ), ); $cxContext = stream_context_create($aContext);  $sFile = file_get_contents("http://www.google.com", False, $cxContext);  echo $sFile; 

Of course, replacing the IP and port of my proxy by those which are OK for yours ;-)

If you're getting that kind of error :

Warning: file_get_contents(http://www.google.com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 407 Proxy Authentication Required 

It means your proxy requires an authentication.

If the proxy requires an authentication, you'll have to add a couple of lines, like this :

$auth = base64_encode('LOGIN:PASSWORD');  $aContext = array(     'http' => array(         'proxy'           => 'tcp://192.168.0.2:3128',         'request_fulluri' => true,         'header'          => "Proxy-Authorization: Basic $auth",     ), ); $cxContext = stream_context_create($aContext);  $sFile = file_get_contents("http://www.google.com", False, $cxContext);  echo $sFile; 

Same thing about IP and port, and, this time, also LOGIN and PASSWORD ;-) Check out all valid http options.

Now, you are passing an Proxy-Authorization header to the proxy, containing your login and password.

And... The page should be displayed ;-)

like image 85
Pascal MARTIN Avatar answered Oct 27 '22 00:10

Pascal MARTIN