troubleshooting Question

How do I get file_get_contents or cURL working behind a proxy?

Avatar of nikez2k4
nikez2k4Flag for United Kingdom of Great Britain and Northern Ireland asked on
PHPMicrosoft IIS Web Server
3 Comments1 Solution6163 ViewsLast Modified:
Hello.

I was wondering if anyone had any advice with using PHP behind a proxy. We have an internal server (running Windows Server 2003) that serves our internal PHP (5.3.3) sites, both internally and externally (via HTTPS). I am trying to get a component called Joomdle working that will connect our Joomla 2.5.1 installation with our Moodle 1.9.12.

Whilst trying to get Joomdle working (which uses either file_get_contents or cURL), I ran into an issue that I haven't experienced before. The functions don't work because of some kind of authentication/authorisation issue and I'm not sure what to do to fix the problem.

I have set up a very simple test.php page that uses file_get_contents to return the data from our internal site (http://portal/) and stores it into a variable that is then echoed. Unfortunately, when I use the following code I get an error message which leads me to believe it could be a proxy server issue:

PHP:
<?php
$data = file_get_contents('http://portal/');
echo $data;
?>
Error:
Warning: file_get_contents(http://portal/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in F:\Inetpub\wwwroot\portal2\test.php on line 2
I also tried forcing the script through our proxy server:

PHP:
<?php
$opts = array('http' => array('proxy' => 'tcp://192.168.0.39:801', 'request_fulluri' => true));
$context = stream_context_create($opts);
$data = file_get_contents('http://portal/', false, $context);
echo $data;
?>
Error:
Warning: file_get_contents(http://portal/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in F:\Inetpub\wwwroot\portal2\test.php on line 4
I have also tried returning the data for http://www.google.com, http://www.bbc.co.uk and our corporate website http://www.landau-forte.org.uk, all of which fail with this error message:

Error:
Warning: file_get_contents(http://www.landau-forte.org.uk/) [function.file-get-contents]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in F:\Inetpub\wwwroot\portal2\test.php on line 2
I have asked our admins to provide me with unfiltered access through the proxy, which they have, but it still returns the same error messages. I have been making the proxy server changes in Internet Options on the server, is this right? Also, just to point out, none of the websites I am trying to get the contents for are password protected and I can access them via IE on the server.

As I will be using third party scripts, I don't want to have to edit the code to include a proxy server when using file_get_contents or cURL.

Does anyone have any advice on how to get around this or do you need any more information?

Many thanks,
James
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros