Site icon WPBrigade

How To Interact With An API Using WordPress Standard Functions?

interact with WordPress APis, how to add a custom field via wordpress

To interact with APIs is a very common thing in Web Development, WordPress gives us the utility functions to interact with APIs. As you know to interact with an API requires good knowledge of cURL and many other things, WordPress takes this responsibility and wrap the inner stories into utility functions for you.

wp_remote_get is the function that gets the data for you from an API. All you have to do is just put the endpoint as an argument and voila! It’s magic.

This function basically takes two arguments
1. $url – Resource to retrieve data from. This must be in a standard HTTP format
2. $args – OPTIONAL – You may pass an array of arguments in here to alter behavior and headers, such as cookies, follow redirects, etc.

Let’s make a call to GitHub API and see what happens.

$response = wp_remote_get( 'https://api.github.com/users/ifiwfi' );

It will retrieve the following data:

Array
(
[headers] => Array
(
[server] => GitHub.com
[date] => Thu, 12 Mar 2015 13:46:33 GMT
[content-type] => application/json; charset=utf-8
[connection] => close
[status] => 200 OK
[x-ratelimit-limit] => 60
[x-ratelimit-remaining] => 58
[x-ratelimit-reset] => 1426171579
[cache-control] => public, max-age=60, s-maxage=60
[last-modified] => Wed, 04 Mar 2015 18:42:08 GMT
[etag] => W/"87972c50f954668862db38480dab5c78"
[vary] => Array
(
[0] => Accept
[1] => Accept-Encoding
)

[x-github-media-type] => github.v3
[x-xss-protection] => 1; mode=block
[x-frame-options] => deny
[content-security-policy] => default-src 'none'
[access-control-allow-credentials] => true
[access-control-expose-headers] => ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval
[access-control-allow-origin] => *
[x-github-request-id] => 6E271E7B:376A:68977C5:550198B8
[strict-transport-security] => max-age=31536000; includeSubdomains; preload
[x-content-type-options] => nosniff
[x-served-by] => 07ff1c8a09e44b62e277fae50a1b1dc4
[content-encoding] => gzip
)

[body] => {"login":"ifiwfi","id":6972531,"avatar_url":"https://avatars.githubusercontent.com/u/6972531?v=3","gravatar_id":"","url":"https://api.github.com/users/ifiwfi","html_url":"https://github.com/ifiwfi","followers_url":"https://api.github.com/users/ifiwfi/followers"}
[response] => Array
(
[code] => 200
[message] => OK
)

[cookies] => Array
(
)

[filename] => 
)

If you want to get the body part from the response, you can do like this:

$response = wp_remote_get( 'https://api.github.com/users/ifiwfi' );
$body = wp_remote_retrieve_body( $response );

If you want to get the response code, you can do like this:

$response = wp_remote_get( 'https://api.github.com/users/ifiwfi' );
$http_code = wp_remote_retrieve_response_code( $response );

The next part of the interaction is to post the values to an API. You can use this function wp_remote_post. This function takes the first argument the URL on which the data has to be posted and the second argument the data which has to be posted. The data should be in the key value pair array. As GitHub doesn’t let us post data so we just assume some URLs.

$body = array(
'name' => 'Irfan Ahmed',
'profession' => 'Software engineer',
'email' => 'irfan.ahmed5123@yahoo.com',
'subject' => 'How to post data to a form or API',
'comment' => 'Hey, I learned a lot from this article.'
);

$args = array(
'body' => $body,
'timeout' => '10',
'redirection' => '3',
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'cookies' => array()
);

$response = wp_remote_post( 'http://yourwebsite.com/contact/', $args );

The last important thing is to make any kind of call. Like you want to delete an object from database table using the API. It will be done like this:

$args = array(
'method' => 'DELETE'
);
$response = wp_remote_request( 'http://api.yourwebsite.com/object/to/delete', $args );

This is the most easiest and interactive way to use an API using WordPress using standard functions. Feel free to comment below and let us know how this tutorial helps you.

Exit mobile version