In this WordPress tutorial, we are going to learn, how to integrate third-party API in WordPress. WordPress has also provided some API’s related to its content. You can integrate third-party API’s with WordPress using PHP curl, but this, not the right way because WordPress already provided some functions for third party API’s.
What is an API?
API stands for Application Programming Interface. Rest API’s allow us to send and receive data across the web.
Why you need third-party API in wordpress
If you want to display your address locations on your WordPress website with a map provided by Google, want to show booking list, want to display the post from social media accounts like facebook and twitter, Or want to post WordPress post to the social media accounts you need integrate their API’s with WordPress. Rest API’s allow us to fetch and post data hosted anywhere on the web within your WordPress website.
Rest API’s Technics
You also need to know Rest API’s terminology. They are followings:-
An API key is the authorization code which is provided by the API service provider.
the process of identifying the user of the API called Authentication.
The client is the service requesters.
The web address (URL) that points to the requested API functionality.
OAuth is an alternate methodology of authentication that grants access while not directly sharing credentials (in distinction to API Keys).
REST (API’s Request Methods)
Representational state transfer a homogenous methodology for interacting with resources via HTTP methods:
Get – for fetch data
Post – for post data
Put – for update data
Delete – for delete data
Let’s start with WordPress third party API’s methods:-
For wp_remote_get request set the body parameters as bellows:-
$url = https://vrsoftcoder.com/"; $bodyRequest = array( "headers"=>array( 'key' => 'your api key', ) ); $api_response = wp_remote_get($url,$bodyRequest);
For wp_remote_post request set the body parameters as bellows:-
$ApiUrl = "https://vrsoftcoder.com/?token="; $bodyRequest = array( "headers"=>array( 'Content-Type' => 'application/x-www-form-urlencoded', 'Ocp-Apim-Subscription-Key' => BOOKER_SUBSCRIPTION_KEY ), "body"=>array( "grant_type"=>"client_credentials", "client_id"=>BOOKER_CLIENT_ID, "client_secret"=>BOOKER_CLIENT_SECRET, "scope"=>BOOKER_SCOPE ) ); $api_response = wp_remote_post($tokenUrl,$bodyRequest);