Understanding WordPress Get API Function

Hi there!

Last week, don’t know why i just decide to create my own first plugin. This plugin will be uploaded into WordPress.org, so anyone can use this plugin for free.

My plugin will retrieve an API response from many servers as your choice and displaying the data at your post/page. If you know about PHP programming and JavaScript/Ajax, the first choice is to use a cURL function which is the basic step to retrieve and convert API response (it could be a JSON or XML format).

And yes, my plugin is working good with that steps. But, the problem came when i submit my plugin to WordPress.org. My plugin was rejected.

They said (the checker team) that my plugin is not using WordPress default functions, and they are not describing what is it.

Then i start checking my plugin codes and also the WordPress codex, i’m also asking to the WordPress plugin forum about this problem.

What i found is: the problem is on my technique (using cURL). WordPress, by default already have a great function named wp_remote_get.

FYI, when you start using a cURL function, your hosting provider have to provide a cURL feature. If they don’t have something like this, your codes/functions will not run. But, by using wp_remote_get, WordPress will search the best method depends on what your hosting provider have.

It is a great feature, then i decide to give it to you something about this function.

To use this function, you can start by this code:

$data = wp_remote_get('http://www.your-api-provider.com');
#1: $response = json_decode( $data['body'] );
#2: $response =  new SimpleXMLElement($data['body']);

foreach ($response->list as $entry):

That is the basic code, you can read the explanation below:

$data: this variable containing the wp_remote_get function with your API provider URL.

#1: $response: Depends on what format did your API provider have. If the return format is a JSON, then you can use json_decode function to parse the data.

#2: $response: Depends on what format did your API provider have. If the return format is a XML, then you can use SimpleXMLElement function to parse the data.

foreach: This is when the parsing start.


Ok, that’s what i’m want to share to you today. If you interested about API programming, you can learn it from wp_remote_get codex at:


Leave a Reply

Please log in using one of these methods to post your comment:

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