If one needs to build a travel product for his own, then SkyScanner is the cheapest, and probably the most efficient option to rely upon. SkyScanner with its powerful API and tools fetches the user with all the information needed for a travel website or app, that including flight searching, flights price comparisons, searching of hotels and cars, listing of hotels from all over the world, car hiring etc. and most importantly, this all at free.
In this tutorial, we will learn to use the SkyScanner API for retrieving the hotels information.
The very first thing to use SkyScanner API is to obtain an API key, which you can easily get after creating an account with SkyScanner.
PHP Code to Get Hotel Information from SkyScanner
SkyScanner first creates a session for any API operation made by the user as the first phase, and then in the next phase allows users to get the information about any of its resources within that session by passing the unique session ID inside the api calling URL, this phase is called Polling of session.
Hotel Details Information:
First you need to get an individual ID for the location, where you need to search the hotels. For this you will have to use Skyscanner Hotels Autosuggest Service API.
Here is the sample code:
1 2 3 4 5 |
$url = 'http://partners.api.skyscanner.net/apiservices/hotels/autosuggest/v2/UK/EUR/en-GB/pari?apikey=<strong>YOUR API KEY HERE</strong>'; $response = file_get_contents($url); $array = json_decode($response, true); echo '<pre>'; print_r($array); |
Note: pari is the keyword for the place , you can use yours like delhi, mascow etc.
I have used UK (market), en_GB (locale) and EUR (currency) in my code, you can replace these with your choice.
Now in the output -> results, select individual_id for the value of your choice and pass it to the next step.
1 2 3 4 |
$id = $array['results'][0]['individual_id']; // to get the individual_id, here I get the very first value $url1 = 'http://partners.api.skyscanner.net/apiservices/hotels/liveprices/v3/UK/EUR/en-GB/'.$id.'/2017-12-04/2017-12-10/2/1?apiKey=YOUR API KEY HERE'; $response1 = file_get_contents($url1); $array1 = json_decode($response1, true); |
This will output the hotels with very limited information. But you can use the hotel Id to get the detailed information for that hotel.
You May Also Like:
Instagram API Integration Into Website
How to Install Magento Security Patch via Putty
Code to Use Paypal Payment Gateway on Website
How to Authenticate with D&B Direct API Web Service
How to Add New Product in Bigcommerce via API V2
How to Connect With Cloudshare Rest API
Here is the Code:
1 2 3 |
$hotel_id1 = $array1['hotels_prices'][0]['id']; // here I am using first two hotel IDs $hotel_id2 = $array1['hotels_prices'][1]['id']; $url_detail = $array1['urls']['hotel_details']; // this is the session url , you need to call to get the detailed hotel information. |
Please notice that $url_detail is the utmost required session URL to poll the session for getting hotel details.
Like this :
1 2 3 4 5 |
$url2 = 'http://partners.api.skyscanner.net'.$url_detail.'&hotelIds='.$hotel_id1.','.$hotel_id2; $response2 = file_get_contents($url2); $array2 = json_decode($response2, true); echo '<pre>'; print_r($array2); |
Here is the Complete Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php $url = 'http://partners.api.skyscanner.net/apiservices/hotels/autosuggest/v2/UK/EUR/en-GB/pari?apikey=YOUR API KEY HERE'; $response = file_get_contents($url); $array = json_decode($response, true); echo '<pre>'; print_r($array); $id = $array['results'][0]['individual_id']; $url1 = 'http://partners.api.skyscanner.net/apiservices/hotels/liveprices/v3/UK/EUR/en-GB/'.$id.'/2017-12-04/2017-12-10/2/1?apiKey=YOUR API KEY HERE'; $response1 = file_get_contents($url1); $array1 = json_decode($response1, true); echo '<pre>'; print_r($array1); $hotel_id1 = $array1['hotels_prices'][0]['id']; $hotel_id2 = $array1['hotels_prices'][1]['id']; $url_detail = $array1['urls']['hotel_details']; $url2 = 'http://partners.api.skyscanner.net'.$url_detail.'&hotelIds='.$hotel_id1.','.$hotel_id2; $response2 = file_get_contents($url2); $array2 = json_decode($response2, true); echo '<pre>'; print_r($array2); ?> |
You are advised to follow the SkyScanner API documentation for using other features of Skyscanner. Using this api service you can get Flight and Cars booking data.
Hi,
thank you for example.
I would like to know where does this code goes if website is devoloped in wordpress.
Is it needed to build a plugin or just some .php fiile?
I would like to know because I would try to build a flight, hotel, car search website with skyscanner api.
Thank you in advance!
Regards!
Marko
Hi Marko,
In wordpress you can create a .php template file under active theme folder. Place skyscanner code in that file.
Than create a new page and use that created template to publish it. You get data on that page.
Thanks
Thank you for the reply.
I’ve have don everything you wrote but there is some error in the template. How the template header should look like?
Regards,
Marko