API script to pull AirlineFlightSchedules


#1

Hello I am new to the php world and coding but not to flying. I am trying to find a script for my API key that I could use to pull certain airline schedules and update them automatically but what is the correct code, I was using the example for php but I am not sure I got it for the second half what do I need to put.

<?php $options = array( 'trace' => true 'exceptions' => 0, 'login' => 'not revealing', 'password' => 'not revealing', ); $client = new SoapClient('http://flightxml.flightaware.com/soap/FlightXML2/wsdl', $options); // get Airline Flight Schedules. $params = array("airline" = "JetBlue"); $result = $client-> I have blanked out my username and api code but other than that what do I need to correct or add to the above code to pull certain airlines schedules?

#2

You’re on the right track. From the point you’ve gotten to, you’ll use the SoapClient object you created ($client in your code snippet above) and call the appropriate method, passing it the appropriate parameters. Check here for a full list of methods you can call. Note that each method may have a specific set of parameters.

That’s the general overview; now let’s look at your specific request. It looks like FleetScheduled is the right choice for the information you’re looking for. You’ll need to know the ICAO prefix for your airline of choice; for JetBlue, that’s JBU.

Here’s a sample snippet to retrieve the first 15 scheduled flights for JetBlue:


<?php

$options = array(
	'trace' => true,
	'exceptions' => 0,
	'login' => '',
	'password' => ''
);
$client = new SoapClient('http://flightxml.flightaware.com/soap/FlightXML2/wsdl', $options);

$params = array(
	'fleet' => 'JBU',	// ICAO prefix for JetBlue
	'howMany' => 15,	// Return 15 results
	'offset' => 0		// Start at the beginning
	// 'offset' => 16		// We could use this to start on the "second page" of results if needed
);

$result = $client->FleetScheduled($params);

// Test to see if the request succeeded
if (is_soap_fault($result)) {
	// Request failed, output error message and quit
	echo 'ERROR: ' . $result->getMessage() . "
";
	die();
} else {
	// Request succeeded, do stuff with the results
	// For example, let's print out a brief summary
	foreach ($result->FleetScheduledResult->scheduled as $flightObj) {
		// Format the departure time nicely
		$friendlyDeparture = date('H:i', $friendlyDeparture);
		echo "$flightObj->ident is a(n) $flightObj->aircrafttype departing $flightObj->origin at $friendlyDeparture
";
	}
	
	// You might find it helpful to look at the raw results as well
	// print_r($result);
}




#3

how about if I wanted to get all of them what would I put as the offset number or the how many?


#4

There are a couple of options. Generally speaking, FlightXML requests return no more than 15 results. However, you can use SetMaximumResultSize to increase the result limit for your account.

However, that’s still a band-aid solution of a sort, because there is no way to simply say “get me all the results.” You could set a really large maximum result size, but in the end you’ll still have to do some checking on your results to ensure you’re not missing anything. Practically, this would take the form of making repeated API requests, each one with a larger offset, until you got a result set smaller than howMany:


/**
 * This is a stub function you might write to do whatever you wish with the results.
 * We put this in a function because we're going to need to call it repeatedly
 */
function processResult($result) {
	// This probably looks familiar :-)
	foreach ($result->FleetScheduledResult->scheduled as $flightObj) {
		// Format the departure time nicely
		$friendlyDeparture = date('H:i', $friendlyDeparture);
		echo "$flightObj->ident is a(n) $flightObj->aircrafttype departing $flightObj->origin at $friendlyDeparture
";
	}
}

// Initial set of params starts at the beginning of the result set
$howMany = 15;
$params = array(
	'fleet' => 'JBU',	// ICAO prefix for JetBlue
	'howMany' => $howMany,
	'offset' => 0
);

// See http://php.net/manual/en/control-structures.do.while.php
do {
	// Fetch and handle the result as before
	$result = $client->FleetScheduled($params);
	
	if (is_soap_fault($result)) {
		// Request failed, output error message and quit
		echo 'ERROR: ' . $result->getMessage() . "
";
		die();
	} else {
		processResult($result);
	}
	
	$resultSetSize = count($result->FleetScheduledResult->scheduled);
	// Increment offset by $howMany
	$params'offset'] = $params'offset'] + $howMany;
} while ($resultSetSize == $howMany); // In plain English: "Keep executing this block of code until we get <15 results back"

You’ll want to be careful with this; I did a test run with that code block and got back 420 results until I stopped it. Running a blanket “get everything” request like this is a pretty quick way to run up a lot of charges.