php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40197 Cannot access remote files
Submitted: 2007-01-22 17:16 UTC Modified: 2016-12-02 11:11 UTC
From: ddrewery at gmail dot com Assigned: tony2001 (profile)
Status: Not a bug Package: HTTP related
PHP Version: 5.2.0 OS: Fedora Core 6
Private report: No CVE-ID: None
 [2007-01-22 17:16 UTC] ddrewery at gmail dot com
Description:
------------
Anytime I try accessing an external file via wrappers (fopen, file_get_contents,...), I get "failed to open stream: HTTP request failed!".    I have "allow_url_fopen = On" and phpinfo() shows this is correct.  It doesn't matter what file, if its not on the local server, it will not work.. even trying to load google.com.. same result.

I have two identical servers, both using FC6.. I upgraded from 5.1.6 to 5.2 on one of them and still have the same issue.  I currenly have ZEND Optomizer enabled but I installed it after this issue stated (thinking it may help).

I can use fsocketopen and make the code work just fine, but anything using fopen wrappers fails.

SELinux is disabled on this machine as well.

http://teton1w.tetonsolutions.com/geo/yh/phpinfo.php


Reproduce code:
---------------
<?php
function loadXFile($location) {
    $q = $location;
    $tmp = file_get_contents($q);
    return $tmp;
}

print loadXFile("http://www.google.com");
?>

Actual result:
--------------
Warning: file_get_contents(http://www.google.com) [function.file-get-contents]: failed to open stream: HTTP request failed! in /home/twhomes/httpdocs/geo/yh/test2.php on line 4


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-22 17:18 UTC] ddrewery at gmail dot com
http://teton1w.tetonsolutions.com/geo/yh/test2.php runs the posted code.
 [2007-01-22 17:23 UTC] tony2001@php.net
Please make sure you don't have any firewalls blocking your connections.
 [2007-01-22 17:51 UTC] ddrewery at gmail dot com
Already checked that.  If it were a firewall issue, I wouldn't be able to do this with fsocketopen().

This works fine: http://teton1w.tetonsolutions.com/geo/yh/test3.php
<?php
function loadXFile($location) {
	$fp = fsockopen($location, 80, $errno, $errstr, 30);
	if (!$fp) {
	   $result = "$errstr ($errno)<br />\n";
	} else {
	   $out = "GET / HTTP/1.1\r\n";
	   $out .= "Host: ".$location."\r\n";
	   $out .= "Connection: Close\r\n\r\n";
	
	   fwrite($fp, $out);
	   while (!feof($fp)) {
		   $result .= fgets($fp, 128);
	   }
	   fclose($fp);
	}
	return $result;
}

print loadXFile("www.google.com")
?>
 [2007-01-22 18:05 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.2-win32-latest.zip


 [2007-01-22 19:09 UTC] ddrewery at gmail dot com
Ok.. I'm now running 5.2.1RC4-dev and still have the same problem.
 [2007-01-22 20:12 UTC] tony2001@php.net
How to reproduce it?
# php -r 'var_dump(strlen(file_get_contents("http://google.com")));'
int(3171)
 [2007-01-22 20:30 UTC] ddrewery at gmail dot com
I run that and get this..

# php -r 'var_dump(strlen(file_get_contents("http://google.com")));'
PHP Warning:  file_get_contents(http://google.com): failed to open stream: HTTP request failed!  in Command line code on line 1

Warning: file_get_contents(http://google.com): failed to open stream: HTTP request failed!  in Command line code on line 1
int(0)
 [2007-01-22 20:44 UTC] tony2001@php.net
Okay, you can reproduce it. But now *I* need to reproduce it.
That was the question - how to reproduce it?
Hint: an account on your machine might be the answer.
 [2007-01-22 20:56 UTC] ddrewery at gmail dot com
Sorry about that.. I can give you root access.  Can I just send it to you @php account?
 [2007-01-22 21:02 UTC] tony2001@php.net
Absolutely no need for root access, just a usual unprivileged account is enough.
Sure, send it to tony2001@php.net.
 [2007-01-22 21:23 UTC] ddrewery at gmail dot com
I emailed you the root password.  I also created an account called tony.
 [2007-01-23 10:29 UTC] tony2001@php.net
The host is behind a Sonicwall.
 [2007-01-23 15:10 UTC] ddrewery at gmail dot com
Ok.. I double checked and I have port 80 wide open in and out on the firewall.  You could not telnet because I have it blocked on all my servers.  I'm watching the firewall logs now and this is what I show.. trying to load a 6000 byte file, it hangs up at 60.

# Source IP  Source Port  Destination IP  Destination Port  Protocol  Src Interface  Dst Interface  Tx Bytes  Rx Bytes  
1 66.210.47.156 32787 66.210.130.9 53 UDP OPT WAN 146 327 
2 66.210.47.156 42404 69.20.65.142 80 TCP OPT WAN 112 60 


.156 is my server, the .9 is the dns lookup, and the .142 is where the image is.
 [2007-01-23 15:26 UTC] tony2001@php.net
>You could not telnet because I have it blocked on 
>all my servers. 
How did you do it?

>trying to load a 6000 byte file, it hangs up at 60.
.. which can hardly be PHP problem.
 [2007-01-23 17:22 UTC] ddrewery at gmail dot com
Ok, its being blocked by the firewall.. however I still confused at why.  Port 80 is wide open and fsockets work fine.

$location = "www.google.com";
$fp = fsockopen($location, 80, $errno, $errstr, 30);
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: ".$location."\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
    $result .= fgets($fp, 128);
}
fclose($fp);
print $result;

That works fine

$location = "http://www.google.com";
$result = file_get_contents($location);

That does not work.  Does file_get_contents() use anything other than port 80 for http requests??
 [2007-01-23 17:39 UTC] tony2001@php.net
>Does file_get_contents() use anything other than port 80 for http requests??
No, of course it does not.
But it uses HTTP/1.0 and I have no idea how exactly your firewall filters the data.
 [2007-01-23 17:46 UTC] ddrewery at gmail dot com
I just change the fsocket example to 

$out = "GET / HTTP/1.0\r\n";

It still works fine.  What method does the wrapper use?  Does it literally open a socket just like fsocketopen would?
 [2007-01-23 17:55 UTC] tony2001@php.net
Let's try another route.
Please try to run the same code on a machine, which is 100% NOT behind a firewall and see if it works for you.

 [2007-01-23 18:03 UTC] ddrewery at gmail dot com
Yes, it works without the firewall.  That is why I agreed with you in my earlier post.  However, my question is now about fsocketopen vs fopen.. what is the actual code that drives file_get_contents()?  There is obviously something differnt in the headers that my firewall doesn't like.. how do I see what headers php is sending?
 [2007-01-23 18:13 UTC] tony2001@php.net
Use a sniffer. tcpdump, for example.
 [2007-01-25 14:54 UTC] ddrewery at gmail dot com
Ok, I finally go ahold of sonic wall tech support and they have resolved the issue.  Maybe you can help me understand what this means.

She had me uncheck a box in the internal settings of the firewall labled "Enforce Host Tag Search with for CFS" and suddently everything works.  She rambled off something about header packet size being limited to 32.. but lost me from there.

With that information, can you tell me what fsocketopen and fopen would be sending differently to change the header response?
 [2007-01-25 15:03 UTC] tony2001@php.net
>She had me uncheck a box in the internal settings of the
>firewall labled "Enforce Host Tag Search with for CFS" 
>and suddently everything works. 
So this is the answer to the question why fsockopen() worked.
As I said, PHP uses HTTP/1.0 for http streams by default and "Host: " header is required only by HTTP/1.1.
 [2016-12-02 09:25 UTC] spargo32 at gmail dot com
I've got the error when I use this
```
D:\Folder>php -r 'file_get_contents("http://packagist.org/p/symfony/var-dumper%24ea3512377b773427554949beddf0ed0f9a6c6f43ba6069419cde361083f78a3c.json");'

Parse error: syntax error, unexpected end of file in Command line code on line 1
```

And everything is well if I replace quotes
```
D:\Folder>php -r "file_get_contents('http://packagist.org/p/symfony/var-dumper%24ea3512377b773427554949beddf0ed0f9a6c6f43ba6069419cde361083f78a3c.json');"
```

What is the problem?
 [2016-12-02 11:11 UTC] requinix@php.net
@spargo32: This is a bug reporting system, not a support forum. http://www.php.net/support.php But I will say that Windows' cmd.exe does not support quoting arguments with apostrophes.
 [2018-03-02 12:46 UTC] hoossam-1987 at hotmail dot com
I AM facing the below issue , only with .pdf https links ,and i am searching from long time , but still i didnt find any solution .
Can i have some one help regarding this case .

Error:
failed to open stream: HTTP request failed!

Source Code :

	$download_link = 'https://www.aramex.com/docs/default-source/resourses/resourcesdata/shipping-services-api-manual.pdf';
 
	// Download File Content
	$file_data = file_get_contents($download_link,1,NULL);
              
 
	// Create File
	$handle = fopen($file_name, 'w');
	fclose($handle);
 
	// Save Content to file
	$downloaded = file_put_contents($file_name, $file_data);
	if($downloaded > 0)
	{
		echo  'file was successfully downloaded! <br>' ;
    }
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 18:01:29 2024 UTC