PHP - Flight Itinirary Api
<?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 .= "¤cy=".$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 Similar TutorialsI need help to get flight schedule from http://www.sriwijayaair.co.id/id but always error using curl to access ssl. here are my code: <?php $headers = array( "User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 GTB7.1 (.NET CLR 3.5.30729)", "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language=en-us,en;q=0.5", "Accept-Encoding=gzip,deflate", "Accept-Charset=ISO-8859-1,utf-8;q=0.7,*;q=0.7", "Keep-Alive=1150", "Connection=keep-alive", "Referer=http://www.sriwijayaair.co.id/id", "Cookie=language=in; location=id; dest=home; __utma=260465999.1394002812.1293106375.1293106375.1293106375.1; __utmb=260465999.1.10.1293106375; __utmc=260465999; __utmz=260465999.1293106375.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)", "Content-Type=application/x-www-form-urlencoded", "Content-Length=121", "POSTDATA=isReturn=false&from=CGK&to=MES&departDate1=30-&departDate2=11-2010&adult=1&child=0&infant=0&returndaterange=0&Submit=Cari" ); // do access to sriwijaya air $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "https://booking.sriwijayaair.co.id/b2c/AvailabilityServlet"); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_VERBOSE, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt"); //curl_setopt($curl, CURLOPT_POSTFIELDS, "isReturn=false&from=CGK&to=MES&departDate1=22&departDate2=01-2011&adult=1&child=0&infant=0&returndaterange=0&Submit=Cari"); $curlData = curl_exec($curl); print_r($curlData); curl_close($curl); ?> any help will be appreciate, thanks Hey everyone, Im writing some code that has to work out a route based on linking airports and output all the flights it takes to get there, tried searching just dont really know what to search for The data table looks like this ID, Origin, Destination, DatetimeDeparting, DatetimeArriving, Price 1, BKK, CNX, 2010-10-14 12:00:00, 2010-10-14 14:00:00, 125 2, STN, BKK, 2010-10-13 18:00:00, 2010-10-13 22:00:00, 300 etc. What is the easiest way of working out the path from STN to CNX via BKK? UPDATE: Just to make it more interesting there will be instances where there is more than 1 path eg. STN -> KUL -> BKK -> CNX |