Hello readers, today I will guide you about "WordPress HTTP API".
Do you wish to make a request to the remote server to go or to post something?
WordPress HTTP API is the perfect solution for this. It provides a simple interface to make remote control requests. It supports methods such as POST, GET, HEAD, PUT, etc. Underneath are some of the assistant functions it provides.
<?php
wp_remote_get() - Retrieves a URL using the GET HTTP method.
wp_remote_post() - Retrieves a URL using the POST HTTP method.
wp_remote_head() - Retrieves a URL using the HEAD HTTP method.
wp_remote_request() - Retrieves a URL using either the default GET or a custom HTTP method.
?>
For what reason to use WordPress HTTP API? There are several ways to hook up and get in touch with any remote sponsor from PHP.
cURL: A library to communicate with remote hosts. But this works only if the cURL extension is packed into PHP.
File-system (fopen, file_get_contents): This function starts up a file or a URL and this works when php ini_settings allows for this.
Electrical sockets (fsockopen): Initiates a plug connection to the reference specified by hostname. Once again it's possible that the Webhost might have impaired the fsockopen function or firewalled outbound requests on whatever port you're attempting to connect.
Streams: Functions on PHP 4. 3 or later.
HTTP: Gives a powerful request and site, a site interface for remote calls. It needs PHP 5. 2+
If you see the above methods, these methods of remote control communication works based on some conditions/situations/hosting environment/php editions and we as a developer need to know/learn lot of things cURL, fopen, fsockopen, php http, php streams and many others. So, while we are creating plugins we have to consider all of the above checks before using these. To make sure a call to remote host works, we need to check if everything is supported on that particular host and php environment and write code accordingly.
WordPress has already done excatly that for us. It does indeed all the checking and use the best method that works. If everything does not work out|neglects|falls flat, then it returns an error(WP_Error).
How to use WordPress HTTP API?
Lets consider an example of getting exact co-ordinates of a location from Google Maps API.
<?php
$address = '144/66, Hathibarkula, Dehradun, India';
$address = urlencode( $address );
$response = wp_remote_get( "http://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "&sensor=true" );
// check the response status code not to be 404
if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
return false;
try {
// get the response body
$result = wp_remote_retrieve_body( $response );
// as the response for Google is in json_encode
$result = json_encode($result);
// as the response for Google is in json_decode
echo $result = json_decode( $result );
} catch ( Exception $ex ) {
echo $result = null;
}
?>
Description of the above code:
— Here in the code above I have used Google MAPs API url as the parameter for wp_remote_get()
$response = wp_remote_get( "http://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "&sensor=true" );
— Check whether the response results in an error OR the response from the remote host has error status code.
if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
— Get the actual response body out of complete response.
$result = wp_remote_retrieve_body( $response );
— Use the response body as you like.
$result = json_decode( $result );
The complete response of the wp_remote_get() looks like this:
{
"results": [{
"address_components": [{
"long_name": "Hathibarkala",
"short_name": "Hathibarkala",
"types": ["political", "sublocality", "sublocality_level_1"]
}, {
"long_name": "Dehradun",
"short_name": "Dehradun",
"types": ["locality", "political"]
}, {
"long_name": "Dehradun",
"short_name": "Dehradun",
"types": ["administrative_area_level_2", "political"]
}, {
"long_name": "Uttarakhand",
"short_name": "UK",
"types": ["administrative_area_level_1", "political"]
}, {
"long_name": "India",
"short_name": "IN",
"types": ["country", "political"]
}],
"formatted_address": "Hathibarkala, Dehradun, Uttarakhand, India",
"geometry": {
"bounds": {
"northeast": {
"lat": 30.3601249,
"lng": 78.06994499999999
},
"southwest": {
"lat": 30.3380879,
"lng": 78.04294469999999
}
},
"location": {
"lat": 30.349283,
"lng": 78.0509886
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 30.3601249,
"lng": 78.06994499999999
},
"southwest": {
"lat": 30.3380879,
"lng": 78.04294469999999
}
}
},
"partial_match": true,
"place_id": "ChIJZb4szS7WCDkRTkD8olmM8-o",
"types": ["political", "sublocality", "sublocality_level_1"]
}],
"status": "OK"
}
Conclusion:
So being a WordPress programmer you should always remember to use wp_remote_get() for making remote requests. WordPress HTTP API is a good solution which works regardless of hosting environment.
0 Comment(s)