Featured Articles

QuickPay & WP e-Commerce Integration

Integration module between QuickPay (Internet Payment Gateway) and the WordPress plugin, WP e-Commer[...]

How to publish on several WordPress Blogs

A friend of mine recently asked me of how to most effectively control content creation on several Wo[...]

Forget cURL, use wp_remote_fopen!

by Lars Koudal on 15/10/2009

71 views so far.

Share

It’s time for another WordPress Development Tip, today is a not so well known function, wp_remote_fopen.

When I search the internet for WordPress tips and tricks, I usually encounter small tutorials and tips that use either file_get_contents or cURL. The first, file_get_contents is a PHP function, which is usually turned off by most web hosting companies.

cURL is an extension built into PHP, and usually works on most web hosts, but it requires a bit more tweaking and coding to use. If you need to send parameters along with your external call, such as login and password for Twitter, or something else, then you are best suited by using cURL for this. (See my other post on how to check if cURL is available: How To Check if the cURL PHP extension is loaded.)

However, if all you want is to download the content of a URL, you should use wp_remote_fopen, which is a built in function in WordPress with redundancy built in.

Redundancy?

Yes, the function first tries to load the external content via fopen (PHP) and then cURL if the first call fails. This means that using this WordPress function ensures it works on most web hosts. (Note: MOST!)

[note]

Returns the contents of a remote URI. Tries to retrieve the HTTP content with fopen first and then using cURL, if fopen can’t be used.

From WordPress Codex
[/note]

How to use?

The function is really easy to use:

 $url = "http://cleverwp.com";
 $content = wp_remote_fopen($url);

BUT, I really recommend you add some error-checking, perhaps something like:

	$url = "http://cleverwp.com";
	$content = wp_remote_fopen($url);
	if ($content<>'') {
		// Do whatever you want with the content here...
	} else {
		// Woops, I think something is going wrong!
	}

I hope you enjoyed this little tip, and perhaps take a look at the other developer tips in the WordPress Development Tips category.

Popular Searches
wp_remote_fopen, curl wordpress, curl wordpress login, wp_remote_fopen($url), using curl with wordpress, wp_remote_fopen not returning results, how to use php curl to login, wordpress wp_remote_fopen, curl availability in wordpress, curl login: wordpress, wordpress plugin curl calls, curl with wordpress, wordpress login with curl, reduancy of curl, wordpress 3.0 curl login, curl load page, post wp via curl, how to call http address in php curl, calling wordpress functions from curl, wordpress curl call, wp_remote_fopen, curl in wordpress, wordpress curl, wordpress curl plugin, using curl to post to pligg,

{ 4 comments… read them below or add one }

Lindskog October 15, 2009 at 12:24

this would load an entire page from the URL from the to the and then you would parse whatever info out of the result?

Reply

myWordPress October 15, 2009 at 13:41

Spot on, my young padiwan :-)

There are also similar functions for RSS feeds, with methods to enable caching so you don’t request information too often from the poor RSS feeds.

Reply

emil August 6, 2010 at 15:23

If you are going to use more php coding and classes like tidy or dom parse anyways, this it might not be that usefull.
Some sites will block you id you dont include an identifyer like CURLOPT_USERAGENT – and how can you include options to wp_remote_fopen?! :)

Reply

Lars Koudal August 6, 2010 at 16:42

Hi Emil

Nice of you to drop by :-)

You are right of course. For more advanced usage wp_remote_fopen() is not powerful enough, and in those rare cases I turn to curl as well.

Reply

Leave a Comment

PRIVACY POLICY: Your privacy is important to us. We will never sell or rent your email address and you can unsubscribe at any time.

Previous post:

Next post: