Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with opening files.

Status
Not open for further replies.

Dustman

Programmer
May 7, 2001
320
US
I've got 2 servers having this same problem. Both are Fedora Core 6 (same install) and php 5.1.6.

Anytime I try accessing an external file, I get "failed to open stream: HTTP request failed!". It doesn't matter how I try it: fopen, file_get_contents, file.. they all give the same problem. 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.

After resaerching for 3 hours.. I've seen several reports of bugs like this but no solutions. I have since then upgraded (built from sorce) to php 5.2.0 and I still have the exact same problem.

The only way I am able to open anything using fwrappers is with fsockets.. this is not a fun solution.

Anybody out there have any clues? I'm running out of links on google that are relevent...

-Dustin
Rom 8:28
 
Anything.. I was originally working on pulling geocoding from yahoo's api but have since then determined that i cannot load any pages.

I'm now just testing on my own servers so I know that there is no restrictions..

This code will not work..
Code:
function getFile($location) {
    //Tried this with fopen, file.. nothing works.
    $result = file_get_contents($location);
    return $result;
}

print "trying<br><br>";
print  getFile("[URL unfurl="true"]www.tetonsolutionsgroup.com");[/URL]
print "<br><br>Done";

It will eventually time out and display
Warning: file_get_contents( [function.file-get-contents]: failed to open stream: HTTP request failed! in test.php on line 4


I convert it over to use fsockets and it loads fine (telling me there is nothing wrong with firewalls or anything..)

Code:
function getFile($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 "trying<br><br>";
print  getFile("[URL unfurl="true"]www.tetonsolutionsgroup.com");[/URL]
print "<br><br>Done";


-Dustin
Rom 8:28
 
Interesting.

Your code does not produce the error I get. When I run your code exactly as you posted it, I get:

[tt]Warning: file_get_contents( [function.file-get-contents]: failed to open stream: No such file or directory in /home/sites/blackwellfamily/html/baz.php on line 4[/tt]

This is expected behavior, since file_get_contents() will interpret " as a filename on the local filesystem (it is a well-formed Linux filename). When I change the line:

print getFile("
to read:

print getFile("
The code works for me.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Sorry.. that was a typo when I transferred it into the forums here.. my code actually has the http:// in there, hince the reason the error has it too.

My original code I'm working with is this:

Code:
function yahoo_geo($location) {  
    $q = '[URL unfurl="true"]http://api.local.yahoo.com/MapsService/V1/geocode';[/URL]
    $q .= '?appid=rlerdorf&location='.rawurlencode($location);
    $tmp = file_get_contents($q);
    libxml_use_internal_errors(true);
    $xml = simplexml_load_string($tmp);
    $ret['precision'] = (string)$xml->Result['precision'];
    foreach($xml->Result->children as $key=>$val) {
	if(strlen($val)){
	    $ret[(string)$key] =  (string)$val;
	}
    }
    return $ret;
}

print yahoo_geo($_REQUEST['a']);

I get this:

Warning: file_get_contents( [function.file-get-contents]: failed to open stream: HTTP request failed! in /home/twhomes/httpdocs/geo/yh/geocode.php on line 6

Warning: Invalid argument supplied for foreach() in /home/twhomes/httpdocs/geo/yh/geocode.php on line 10
Array

You can see this yourself with this link:
-Dustin
Rom 8:28
 
Sorry.. I didn't see the dns question earlier.. I haven't noticed any other dns issues.. I would think the fsockets exaple would rule that out.. if it were dns related, that method would not work.

I've got a phpinfo page out there too if you need to take a look.


-Dustin
Rom 8:28
 
It must be something with your PHP configuration. I'm running PHP version 5.2.0 and this version of your code:

Code:
<?php
function yahoo_geo($location) {
    $q = '[URL unfurl="true"]http://api.local.yahoo.com/MapsService/V1/geocode';[/URL]
    $q .= '?appid=rlerdorf&location='.rawurlencode($location);
    $tmp = file_get_contents($q);
        return $tmp;
}

print yahoo_geo($_REQUEST['a']);

returns on my system:

[tt]<?xml version="1.0"?>
<ResultSet xmlns:xsi=" xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps precision="address"><Latitude>35.664063</Latitude><Longitude>-97.485432</Longitude><Address>240 BARRETT PL</Address><City>EDMOND</City><State>OK</State><Zip>73003-5230</Zip><Country>US</Country></Result></ResultSet>
<!-- ws02.search.re2.yahoo.com uncompressed Sun Jan 21 15:34:15 PST 2007 -->[/tt]

But having compared the output of a phpinfo() script on my system and yours, I'll be darned if I can find anything significant in the differences between the two configurations.


Want the best answers? Ask the best questions! TANSTAAFL!
 
A little more info.. I copied this same code over to an older server running RHEL 4 and php 4.3.4 and it works fine. The XML functions don't work but I can succesfully make it print out what it fetches..


I also tried this from the shell.. same problem. For kicks I also ran curl from the command line just to rule out lookup issues:

Code:
[root@teton1w yh]# curl "[URL unfurl="true"]http://api.local.yahoo.com/MapsService/V1/geocode?appid=rlerdorf&location=240%20Barrett%20PL%2C%20Edmond%2C%20OK%2073003"[/URL]
<?xml version="1.0"?>
<ResultSet xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps [URL unfurl="true"]http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd"><Result[/URL] precision="address"><Latitude>35.664063</Latitude><Longitude>-97.485432</Longitude><Address>240 BARRETT PL</Address><City>EDMOND</City><State>OK</State><Zip>73003-5230</Zip><Country>US</Country></Result></ResultSet>
<!-- ws02.search.re2.yahoo.com uncompressed/chunked Sun Jan 21 15:46:33 PST 2007 -->
[root@teton1w yh]#

-Dustin
Rom 8:28
 
This is driving me absolutly insane.. any idea how fopen wrappers actually work? Is there a temporary file somewhere possbily thats not gettin written? I'm watching the error logs and I don't see any hints at all.

I also just installed the zend optomizer.. sure helps the speed of my other pages but still no change on this problem.

-Dustin
Rom 8:28
 
If you login as root on your system, then su to the user as which Apache runs, then try to run the script from the command-line, does it work?

And what are your SELinux settings?


My thinking here is that some mechanism is not letting certain users get out of the system.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Apache runs as Apache:Apache.. i cannot su to apache.. i tried changing it to a different user and still had the same problem

As far as SELinux goes.. I've actually got it disabled on this server.

-Dustin
Rom 8:28
 
Unfortunatly I am too.. thanks for trying though. I'll make sure I post my findings whenever I finally figure it out.

-Dustin
Rom 8:28
 
Just an update here.. after getting some help from the php guys, we've determined that it has something to do with the sonic wall firewall I'm running through. I was able to bypass the firewall and it works just fine. However, now I'm faced with another delima.

On my firewall, I have port 80 wide open for this server, in and out. Using tcpdump, I see the request being sent out but never get a response when I'm using fopen (or any of the likes). I change over to fsocketopen and viola.. I see packets going in and out.. no problem at all. I've been digging around for a day now trying to figure out what could be different from using the fopen wrappers and manually opening a socket.. so far, I see nothing at all.


-Dustin
Rom 8:28
 
AHA.. finally an answer from SonicWall tech support. Just in case somebody else like me searches and finds this, I'll give all the details of how to fix it.

She had me log into my sonicwall and then change the url from main.html to diag.html (no other way to get to this page.. convenient isn't it..). I click a button that says "internal settings" and unchecked a box labeled "Enforce Host Tag Search with for CFS". After applying this, EVERYTHING WORKS!!!

She rambled off some explanation about default header size being limited to 32 characters or something and that with that unchecked it will allow any size.. I don't know what that means and the geek inside of me would love to know, however, after 4 long days ending with a 30 second fix.. I'm just happy to call it done.

If her explanation makes sense to anybody, feel free to elaborate.

-Dustin
Rom 8:28
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top