Jump to content

Flight Itinirary API


oracle765

Recommended Posts

<?php

function extractItem(&$array, $itemKey)
{
	$data = array();
        
	foreach($array[$itemKey] as $item)
	{
              $Id = $item['Id'];  // get the id
              unset($item['Id']); // remove the id from item array
              $data[$Id] = $item; // set id as the key
	}

	unset($array[$itemKey]); // remove item from array.

	return $data; // return the new item array
} 


// get the api parametures from setup url
    $country_code = $_GET['country'];
    $originplace  = $_GET['strtplace'];
    $destination  = $_GET['endplace'];
    $start_date   = $_GET['startdate'];
    if(isset($_GET['enddate']) && $_GET['enddate']!=""){
    $end_date     = $_GET['enddate'];}
    $audult       = $_GET['audult'];
    if(isset($_GET['child']) && $_GET['child']!=""){
    $child        = $_GET['child'];}
    if(isset($_GET['infent']) && $_GET['infent']!=""){
    $infent       = $_GET['infent'];}
    $class        = $_GET['class'];
    
    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
    $ip=$_SERVER['HTTP_CLIENT_IP'];}
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];}
    else{$ip=$_SERVER['REMOTE_ADDR'];}
    $xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".$ip);
    $ip_curency = $xml->geoplugin_currencyCode ;

    $query  = "&country=".$country_code;
    $query .= "&currency=".$ip_curency;
    $query .= "&locale=en-GB";
    $query .= "&originplace=".$originplace;
    $query .= "&destinationplace=".$destination;
    $query .= "&outbounddate=".$start_date;
    if(isset($end_date) && $end_date!=""){
    $query .= "&inbounddate=".$end_date;}
    $query .= "&adults=".$audult;
    if(isset($child) && $child!=""){
    $query .= "&children=".$child;}
    if(isset($infent) && $infent!=""){
    $query .= "&infants=".$infent;}
    $query .="&locationschema=iata";
    $query .="&cabinclass=".$class;
    $query .="&pagesize=10";

$apiParamsUrl = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/".$country_code."?apikey=SECRET".$query."";
$apiParamsStr = parse_url($apiParamsUrl, PHP_URL_QUERY); // get the query string parametures
parse_str($apiParamsStr, $apiParamsArray); // parse into an array

// the api url. First we need to request for a session
$apiSessionUrl = 'http://partners.api.skyscanner.net/apiservices/pricing/v1.0';

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $apiSessionUrl);
curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Accept: application/json')); // make api return json data
curl_setopt($ch,CURLOPT_POST, count($apiParamsArray)); // set how many fiels
curl_setopt($ch,CURLOPT_POSTFIELDS, $apiParamsStr);    // set the fields

// caputre the headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

//execute post
$response = curl_exec($ch);

// get the headers
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);

//close connection
curl_close($ch);

// get the api session url
preg_match('~Location: ([^\s]+)~', $header, $matches);
$apiSessionUrl = $matches[1];

// add on the api key for the session
$apiSessionUrl .= '?apiKey=' . $apiParamsArray['apikey'];

// get the json data
$data = file_get_contents($apiSessionUrl);

// decode the json
$array = json_decode($data, true);

// dump json array
printf('<pre>Poll Data  %s</pre>', print_r($array, true));

//Itineraries
		$Agents   = extractItem($array, 'Agents');   // get array of agents
//...

	foreach ($array['Itineraries'] as $Itineraries) {
        foreach($Itineraries['PricingOptions'] as $Option) {
             echo "<b> Agents: </b> ";

             // loop over agents for current priceOption
             foreach($Option['Agents'] as $agentId) {
                  echo $Agents[ $agentId ]['Name']. ', '; // echo the agent name that matches agentId.
             }
               
             echo " <br> price </b>",$Option['Price'] ,"<br />";
       }
	}
		
		//Legs
		foreach ($array['Legs'] as $Leg) {
		
			echo "<b> Departure </b> " .$Leg['Departure'], " <br> <b>Arrival </b>",$Leg['Arrival'] ,"<br />";
		}
		
		//Carriers
		foreach ($array['Carriers'] as $Carrier) {
		
				echo "<b> Name </b> " .$Carrier['Name'], " <br> Image </b>",$Carrier['ImageUrl'] ,"<br />";
		}
		
		//Places
		foreach ($array['Places'] as $Place) {
		
			echo "<b> Code </b> " .$Place['Code'], " <br> Name </b>",$Place['Name'] ,"<br />";
		}
?>

Hi professionals

 

I am trying to extract the correct information from a flights API

 

It was working until I added in the function.

 

Now I just see the word Poll Data on my screen

 

is there something I am missing here (code attached)

 

Thanks

Link to comment
Share on other sites

<?php

function extractItem(&$array, $itemKey)
{
	$data = array();
        
	foreach($array[$itemKey] as $item)
	{
              $Id = $item['Id'];  // get the id
              unset($item['Id']); // remove the id from item array
              $data[$Id] = $item; // set id as the key
	}

	unset($array[$itemKey]); // remove item from array.

	return $data; // return the new item array
} 


// get the api parametures from setup url
    $country_code = $_GET['country'];
    $originplace  = $_GET['strtplace'];
    $destination  = $_GET['endplace'];
    $start_date   = $_GET['startdate'];
    if(isset($_GET['enddate']) && $_GET['enddate']!=""){
    $end_date     = $_GET['enddate'];}
    $audult       = $_GET['audult'];
    if(isset($_GET['child']) && $_GET['child']!=""){
    $child        = $_GET['child'];}
    if(isset($_GET['infent']) && $_GET['infent']!=""){
    $infent       = $_GET['infent'];}
    $class        = $_GET['class'];
    
    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
    $ip=$_SERVER['HTTP_CLIENT_IP'];}
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];}
    else{$ip=$_SERVER['REMOTE_ADDR'];}
    $xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".$ip);
    $ip_curency = $xml->geoplugin_currencyCode ;

    $query  = "&country=".$country_code;
    $query .= "&currency=".$ip_curency;
    $query .= "&locale=en-GB";
    $query .= "&originplace=".$originplace;
    $query .= "&destinationplace=".$destination;
    $query .= "&outbounddate=".$start_date;
    if(isset($end_date) && $end_date!=""){
    $query .= "&inbounddate=".$end_date;}
    $query .= "&adults=".$audult;
    if(isset($child) && $child!=""){
    $query .= "&children=".$child;}
    if(isset($infent) && $infent!=""){
    $query .= "&infants=".$infent;}
    $query .="&locationschema=iata";
    $query .="&cabinclass=".$class;
    $query .="&pagesize=10";

$apiParamsUrl = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/".$country_code."?apikey=SECRET".$query."";
$apiParamsStr = parse_url($apiParamsUrl, PHP_URL_QUERY); // get the query string parametures
parse_str($apiParamsStr, $apiParamsArray); // parse into an array

// the api url. First we need to request for a session
$apiSessionUrl = 'http://partners.api.skyscanner.net/apiservices/pricing/v1.0';

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $apiSessionUrl);
curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Accept: application/json')); // make api return json data
curl_setopt($ch,CURLOPT_POST, count($apiParamsArray)); // set how many fiels
curl_setopt($ch,CURLOPT_POSTFIELDS, $apiParamsStr);    // set the fields

// caputre the headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

//execute post
$response = curl_exec($ch);

// get the headers
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);

//close connection
curl_close($ch);

// get the api session url
preg_match('~Location: ([^\s]+)~', $header, $matches);
$apiSessionUrl = $matches[1];

// add on the api key for the session
$apiSessionUrl .= '?apiKey=' . $apiParamsArray['apikey'];

// get the json data
$data = file_get_contents($apiSessionUrl);

// decode the json
$array = json_decode($data, true);

// dump json array
printf('<pre>Poll Data  %s</pre>', print_r($array, true));

//Itineraries
//		$Agents   = extractItem($array, 'Agents');   // get array of agents
//...

	foreach ($array['Itineraries'] as $Itineraries) {
        foreach($Itineraries['PricingOptions'] as $Option) {
             echo "<b> Agents: </b> ";

             // loop over agents for current priceOption
             foreach($Option['Agents'] as $agentId) {
                  echo $Agents[ $agentId ]['Name']. ', '; // echo the agent name that matches agentId.
             }
               
             echo " <br> price </b>",$Option['Price'] ,"<br />";
       }
	}
		
		//Legs
		foreach ($array['Legs'] as $Leg) {
		
			echo "<b> Depart </b> " .$Leg['Departure'], " <b> Arrive </b>",$Leg['Arrival'] ,"  <b> Flight Duration </b>", $Leg['Duration'] , " Minutes, <b> Number of Stops </b>", $Leg['Stops'] , "<br />";
		}
		
		//Carriers
		foreach ($array['Carriers'] as $Carrier) {
		
				echo "<b> Name </b> " .$Carrier['Name'], " <b> Image </b>", $Carrier['ImageUrl'] ,"<br />";
				
		}
		
		//Places
		foreach ($array['Places'] as $Place) {
		
			echo "<b> Place Code </b> " .$Place['Code'], " <b> Place Name </b>",$Place['Name'] ,"<br />";
		}
?>

Hi all

 

I bit of an update

 

 

I have managed to extract a bit more information at the bottom as you can see at the bottom of the array.

 

 

2 things I still seem t be struggling with are

 

1:  the function call I have commented out because I cannot figure why its not working

 

2:  I obviously want to present this nicely EG

 

 

6:35 am - 10:40 am    21hrs 5 mins, 1 stop Price $2560.24

 

BNE        LAX                                    SYD

Brisbane Los Angeles                       Sydney

 

 

 

 

something like the way expedia do it which I have attached an example

 

please see my updated code, im a bit lost with it now

 

 

thanks all

 

 

you can also see working text here dev DOT compareandchoose DOT com DOT au/sky/Alan_test

 

I hope I have provided enough information

post-166889-0-09513400-1405160582_thumb.png

Link to comment
Share on other sites

foreach ($array['Itineraries'] as $Itineraries) {
        foreach($Itineraries['PricingOptions'] as $Option) {
             echo "<b> Agents: </b> ";

             // loop over agents for current priceOption
             foreach($Option['Agents'] as $agentId) {
                  echo $Agents[ $agentId ]['Name']. ', '; // echo the agent name that matches agentId.
             }
               
             echo " <br> price </b>",$Option['Price'] ,"<br />";
			 
       }
	}
		
		//Legs
		foreach ($array['Legs'] as $Leg) {
		
			echo "<b>   Depart </b> " .$Leg['Departure'], " <b>   Arrive </b>",$Leg['Arrival'] ," <b>   Direction </b>",$Leg['Directionality'] ,"  <b> Flight Duration </b>", $Leg['Duration'] , "  Minutes, <b>   Number of Stops </b>", $Leg['Stops'] , "<br />";
		}

I have managed to get a little further if anyone can help me figure out this looping problem

 

it loops through the agents so agent 1 would match leg 1, agent 2 would match leg 2 and so on but I have this code which loops through all the agents and then all the legs after that but I want to do agent 1, leg1 as stated

 

 

Please any help would be a massive hurdle for me

 

thanks in advance

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.