file_get_contents behind a proxy? - php

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.

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 ;-)

Use stream_context_set_default function. It is much easier to use as you can directly use file_get_contents or similar functions without passing any additional parameters
This blog post explains how to use it. Here is the code from that page.
<?php
// Edit the four values below
$PROXY_HOST = "proxy.example.com"; // Proxy server address
$PROXY_PORT = "1234"; // Proxy server port
$PROXY_USER = "LOGIN"; // Username
$PROXY_PASS = "PASSWORD"; // Password
// Username and Password are required only if your proxy server needs basic authentication
$auth = base64_encode("$PROXY_USER:$PROXY_PASS");
stream_context_set_default(
array(
'http' => array(
'proxy' => "tcp://$PROXY_HOST:$PROXY_PORT",
'request_fulluri' => true,
'header' => "Proxy-Authorization: Basic $auth"
// Remove the 'header' option if proxy authentication is not required
)
)
);
$url = "http://www.pirob.com/";
print_r( get_headers($url) );
echo file_get_contents($url);
?>

Depending on how the proxy login works stream_context_set_default might help you.
$context = stream_context_set_default(
array(
'http'=>array(
'header'=>'Authorization: Basic ' . base64_encode('username'.':'.'userpass')
)
)
);
$result = file_get_contents('http://..../...');

There's a similar post here: http://techpad.co.uk/content.php?sid=137 which explains how to do it.
function file_get_contents_proxy($url,$proxy){
// Create context stream
$context_array = array('http'=>array('proxy'=>$proxy,'request_fulluri'=>true));
$context = stream_context_create($context_array);
// Use context stream with file_get_contents
$data = file_get_contents($url,false,$context);
// Return data via proxy
return $data;
}

Related

Using another proxy every php request (curl / file_get_contents)

Trying to get contents of a url, but to avoid getting blocked i want to use a proxy every request.
But both ways do not seem to work...
EDIT:
Now i tried this, but my server log keeps logging my real IP.
$page = file_get_contents("https://free-proxy-list.net/");
preg_match_all("/[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}<\/td><td>[0-9]{1,5}/", $page, $matches);
$randomproxy = $matches[0][array_rand($matches[0])];
$randomproxy = "tcp://".str_replace("</td><td>", ":", $randomproxy);
echo $randomproxy;
// configure default context to use proxy
$opts = array(
'tcp' => array(
'proxy' => $randomproxy
)
);
$context = stream_context_create($opts);
$sFile = file_get_contents("https://www.website.tld/inner.html", False, $context);
var_dump($sFile);

Trying to pass htaccess and login into redmine api

I want to connect to the Redmine API. The page is protected by a .htaccess file.
As long as the user credentials (name + password) are the same for .htaccess and Redmine, there aren't any problems.
Well... Two of my teammates are using different credentials (their error code: 401, not authorized) and they just don't want to change them.
Actually, I think this is because they don't pass .htaccess (with their credentials) or they pass .htaccess but aren't able to use Redmine with their data.
I was searching for hours (well, at least since 12:30 o'clock) but couldn't resolve this problem. I just can't figure out how to pass the .htaccess and API credentials correctly.
My Code:
$sURL = 'https://myserver/redmine/';
$sAction = 'projects.json';
$sApiUser = 'user_name';
$sApiPass = 'user_pass';
$sHtAccUser = 'htacc_user';
$sHtAccPass = 'htacc_pass';
#$sURL = sprintf('https://%s:%s#myserver/redmine/',$sUser,$sPass);
$aOptions = array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_USERPWD => $sUser.':'.$sPass,
CURLOPT_PORT => 443,
CURLOPT_CONNECTTIMEOUT => 4,
CURLOPT_TIMEOUT => 6,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_HEADER => FALSE,
#CURLOPT_HTTPHEADER => array('Authorization: Basic '.base64_encode($sHtAccUser.':'.$sHtAccPass)),
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_URL => $sURL.$sAction.$sGetParamQuery);
$ch = curl_init();
if($ch === false)
{
die('Failed to create curl object');
}
curl_setopt_array($ch,$aOptions);
$mResult = curl_exec($ch);
$aCurlInfo = curl_getinfo($ch);
$iHTTPStatus = (int) $aCurlInfo['http_code'];
curl_close($ch);
So, after I had a conversation with someone who is more experienced, I could solve the problem "myself".
Actually, htaccess and redmine use the same authentication method (HTTP Basic authentication), which can't used twice. For this reason, I have to use the individual API key (which you can found at /my/account, on your own redmine).
As long as both credentials (htaccess + redmine) are the same, you don't need the API key.
If the credentials are different... well, you have to login into redmine, get your API key and use it. Maybe not a comfortable way but actually the way to go.

Using get_headers with a proxy

In order to check if an URL is an image, I use the PHP function get_headers. In normal conditions, it works very well.
But when I'm behind a proxy, it causes a timeout exception. I had the same problem with file_put_contents but I solved it by adding a context parameter. However, the get_headers function hasn't a similar argument.
Do you know how to do please ?
Use stream_context_set_default function.
This blog post explains how to use it. Here is the code from that page.
<?php
// Edit the four values below
$PROXY_HOST = "proxy.example.com"; // Proxy server address
$PROXY_PORT = "1234"; // Proxy server port
$PROXY_USER = "LOGIN"; // Username
$PROXY_PASS = "PASSWORD"; // Password
// Username and Password are required only if your proxy server needs basic authentication
$auth = base64_encode("$PROXY_USER:$PROXY_PASS");
stream_context_set_default(
array(
'http' => array(
'proxy' => "tcp://$PROXY_HOST:$PROXY_PORT",
'request_fulluri' => true,
'header' => "Proxy-Authorization: Basic $auth"
// Remove the 'header' option if proxy authentication is not required
)
)
);
$url = "http://www.pirob.com/";
print_r( get_headers($url) );
echo file_get_contents($url);
?>

Basic authentication with Apache

I have basic authentication enabled on an Apache server. The server is hosting an API that I implemented and I want to do is call this API from my PHP script. I got as far as figuring out how to create a header:
$user = 'my_name_api';
$pwd = 'xxxxxxxxx';
$auth_string = $user . ':' . $pwd;
$auth_b64 = base64_encode($auth_string);
$header = 'Authorization: Basic ' . $auth_b64;
How do I include the $header in my API calls? I am looking for something other than cURL, and I am NOT using any environments like zend, etc. (saw some example for Zend, etc., but I am not using any of those).
Have you tried fopen / file_get_contents?
Start here: http://php.net/manual/en/function.fopen.php
or here: http://www.php.net/manual/en/function.file-get-contents.php
and let me know how it goes...
airyt
If you don't need SSL or proxy support, you can use file_get_contents() with a stream context. The stream context can contain HTTP headers:
$opts = array
(
'http'=>array
(
'method' => "GET",
'header' => "Authorization: ..."
)
);
$context = stream_context_create($opts);
$file = file_get_contents('http://www.example.com', false, $context);
More on this here.

php http request

$post_data = array(
'url' => $all[2],
'op' => 'sv',
'sid' => 1
);
// Send a request to example.com
$result = post_request('http://www.yahoo.com', $post_data);
function PostRequest($url) {
$opts = array('http' =>
array(
'method' => 'GET',
'header' => "Content-type: application/x-www-form-urlencoded\r\n"."Accept-language: en\r\n" .
"Cookie: member_id=8593099\r\n" .
"Cookie: pass_hash=fad917fe75e1059f85fc6d9bb6f7a19f\r\n".
"Cookie: session_id=279fe56fd87e5371dc7e1c9f66c27522"
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
return $result;
}
I am able to send the request, but my action needs login to be performed.
Even once I'm logged in, it classifies me as not logged in.
I'm using localhost to send out the request. Is that because of the different domain?
I already copied the login cookies for my localhost, but it is still not working.
Any ideas?
What I tried to do is send http request with php.
My request has sent out, but my destination cannot detect cookies, and claim I am not login.
I'm not too sure what you're trying to accomplish, as the code you've posted isn't quite clear. post_request() isn't a native PHP-function, so you'd have to give us a sample of it for us to be able to help you further.
I would however recommend that you've check that you've put session_start(); way up top in your PHP-files - it ensures that you're able to access the session/cookie.

Categories