The Geekdom

Linux

PHP file_get_contents problem

by linuxnoob on Mar.06, 2009, under Linux

It became necessary today to modify the default gw on one of our servers to route the server through a NAT subnet, as opposed to the network of the live IP for the box. At this point all is well.
I tossed together a quick php script that read the contents of an http address to achieve the goal of pulling geolocation coordinates from the yahoo geolocation API. This had been working flawlessly before the change. After the change however, the script began receiving HTTP timeouts. Hmm.

After hours of investigation my Admin partner in crime and I decided that it must be some sort of issue with our firewall. The odd thing was though, curl works flawlessly. So I modified my script to use the PHP curl library, and everything returned to working order.

I can only theorize that somehow fsockopen was either being blocked by our firewall, or was not passing proper information to the HTTP server to get a response back. Curl however negotiates the outbound connection properly and is able to retrieve the information.
If anyone has any clarification on this matter, I would greatly appreciate it. But in short, if your script using file_get_contents or fsockopen fails with the “failed to open stream: HTTP request failed! ” error, try using php curl lib.

I’ll write up another post with before and after examples soon.

Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

    Archives

    All entries, chronologically...