Fetch URL Content using PHP


I recently had to develop a small script that will fetch an XML file from the web. All I had to do is download a given URL and read its contents. To my great surprise I found that download the file using my jx Ajax library was much easier than doing it with PHP.

PHP make this very easy by including functions like file_get_contents() that has URL support. This code will get you the contents of an URL.

$contents = file_get_contents('http://example.com/rss.xml');

Unfortunately, this is a huge security threat – and many servers have disabled this feature in PHP. Also this is not the most optimized method to fetch an URL. Also, it is impossible to submit data using the POST method using this function.

PHP provide other two method to fetch an URL – Curl and Fsockopen. But to use them you have to be aware about both these libraries and you need to write down a lot of code.

Finally I found on net a very cool PHP function  load() which is developed by Binny. You can visit his site

http://www.bin-co.com/php/scripts/load/

Using this load() function you can easily fetch the URL content. It returns the data as an object.

Hat’s off Binny 🙂 gud job

cheers,

jaswant

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s