How to consume external REST services from within WordPress

As shortly discussed with @Jose in the last Coding chat session, I would like to consume REST API services from within WordPress.
This is the API I am looking for: Expedia EAN (http://developer.ean.com/docs/room-avail).
I would like to set the parameters of the REST call based on user settings, e.g. taking the dates from a calendar control.
The results from the call should then be parsed and be displayed in a table.
As I am new to WordPress developing I would appreciate any advice I can get from you, e.g. are there plugins which could to the heavy lifting or where should I look for a starting point regarding development.

Thank you!

  • Jose

    Hi there Alexander,

    I was taking a look at Expedia API docs and it is totally doable, while it would need some good amount of coding and testing.

    From a wordpress perspective, you should look at the HTTP API methods: https://codex.wordpress.org/HTTP_API

    This methods will allow you to easily retrieve data from the restful API.
    For instance, assuming that you are using the JSON API, you can use something like this:

    $api_request    = 'http://api.ean.com/ean-services/rs/hotel/v3/avail?cid=55505&minorRev=99&apiKey=cbrzfta369qwyrm9t5b8y8kf&locale=en_US...';
    $api_response = wp_remote_get( $api_request );
    $api_data = json_decode( wp_remote_retrieve_body( $api_response ), true );

    In this example, $api_data will be a stdClass object containing all the information returned by the API.

    Of course, this is far more complex than that. You should first build the $api_request using user inputs, cid, apiKey, and generated signature.
    Also, you should add some logic to check for errors in the API response.

    For the signature generation, you can find a PHP example here: http://developer.ean.com/docs/getting-started/api-authentication

    As I am new to WordPress developing I would appreciate any advice I can get from you, e.g. are there plugins which could to the heavy lifting or where should I look for a starting point regarding development.

    By looking at the plugin catalog in wp.org, I found this one:
    https://wordpress.org/plugins/online-booking-engine/
    It doesn't seems to be exactly what you are looking for, but maybe you can learn something from their code.

    Hope this helps you as a start point.
    Please let me know if you have any specific question and I'll be glad to help. :slight_smile:

    Cheers,
    Jose

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.