Andrew Powell

Into The Mind of A Solutions Architect

Andrew Powell

Announcing The Kayak ColdFusion API

March 17, 2008 · No Comments

Beta 1 Release I don't know about you, but when I travel, I like to get the best price. However, I can't stand digging around Orbitz, Expedia, etc. to find the best price. Kayak is a great little service that will search all those sites, plus the airlines' sites for you and eliminate that headache. Recently, they released an API to access their service. I decided to take it upon myself to build a ColdFusion interface that abstracts the base HTTP calls and does the XML parsing for you. As a developer, all you need to deal with is native ColdFusion objects. It is now VERY simple for ColdFusion developers to build this travel search functionality into their applications. There is a single driver CFC that is the one you need to use directly in your applications: net.infoaccelerator.travel.kayak.Kayak . It contains all the base functionality. Additional functionality is provided in the form of data gateways to make airline and/or airport lookup calls, and an AJAX service for autosuggested airport lookups (included in sample). Also included in the package is WDDX formatted data containing every airport and airline that Kayak supports. These WDDX files are the basis for the data pulled from the DGs or AJAX service. Currently, only two types of searches are available via the API from Kayak: Hotel and Flight, so naturally, these are the only searches supported in the ColdFusion API. As Kayak adds searches to the API, this will be updated. What I'm looking for from you, the community, is beta testing. Download the component, put it through its paces and submit bugs to my google code site. This should work on everything from CFMX to CF8, but I've only had the chance to test against CF8. Sample Flight Search: <!--- Instantiate the kayak component --->
<cfset kayakObj    = createObject('component','net.infoaccelerator.travel.kayak.Kayak').init("YOUR DEVELOPER KEY") />

<!--- Get the session id. This is required for each search. --->
<cfset kSessionID    = kayakObj.getSession()/>

<!--- Start the search process --->
<cfset searchStart    = kayakObj.startFlightSearch(false,FORM.origin,FORM.destination,FORM.depart_date,FORM.return_date,FORM.depart_time,FORM.return_time,FORM.travelers,FORM.cabin,kSessionID)/>


<cfif searchStart.status EQ "ok">
   <!--- This call polls the kayak service and aggregates the search and its results into a FlightSearchResult object' --->
   <cfset flightresults    = kayakObj.getFlightResults(searchStart.id,kSessionID,30,'normal','','up',FORM.sort,searchStart.cookies)/>
   
   <cfloop from="1" to="#arrayLen(flightResults.trips)#" index="i">
      <div>
      <p><cfoutput>#lsCurrencyFormat(flightResults.trips[i].price.value)# - <a href="http://api.kayak.com#flightResults.trips[i].price.url#">Book It</a></cfoutput></p>
      <ul>
      <cfloop from="1" to="#arrayLen(flightResults.trips[i].legs)#" index="j">
         <cfoutput>
            <li>
               <p>Airline: #flightResults.trips[i].legs[j].airlineName# - Stops: #flightResults.trips[i].legs[j].stops#<br/>
                Departs: #flightResults.trips[i].legs[j].depart#<br/>
                Arrives: #flightResults.trips[i].legs[j].arrive#</p>
               <ol>
                  <cfloop from="1" to="#arrayLen(flightResults.trips[i].legs[j].segments)#" index="k">
                     <li>
                        #flightResults.trips[i].legs[j].segments[k].airline##flightResults.trips[i].legs[j].segments[k].flight#<br/>
                        Departs #flightResults.trips[i].legs[j].segments[k].origin#: #flightResults.trips[i].legs[j].segments[k].departure#<br/>
                         Arrives #flightResults.trips[i].legs[j].segments[k].destination#: #flightResults.trips[i].legs[j].segments[k].arrival#
                     </li>
                  </cfloop>
               </ol>
            </li>   
         </cfoutput>
         
      </cfloop>
      </ul>
      </div>
      <hr/>
   </cfloop>
   
   <cfelse>
      <!--- If our search is not OK, then we get a msg back to explain the failure. --->
      <cfoutput>#searchStart.msg#</cfoutput>
</cfif>
Sample Hotel Search: <!--- Instantiate the kayak component --->
<cfset kayakObj    = createObject('component','net.infoaccelerator.travel.kayak.Kayak').init("YOUR DEVELOPER KEY") />

<!--- Get the session id. This is required for each search. --->
<cfset kSessionID    = kayakObj.getSession()/>

<!--- Start the search process --->
<cfset results       = kayakObj.startHotelSearch(FORM.othercity,FORM.checkin_date,FORM.checkout_date,FORM.guests1,FORM.rooms,kSessionID)      />

<cfif results.status EQ "ok">
   <!--- This call polls the kayak service and aggregates the search and its results into a HotelSearchResult object' --->
   <cfset hotelResult    = kayakObj.getHotelResults(results.id,kSessionID,30,'normal',0,'up',FORM.sort,results.cookies)/>
   
   <cfloop from="1" to="#arrayLen(hotelResult.hotels)#" index="i">
      <div>
      <p><cfoutput>#lsCurrencyFormat(hotelResult.hotels[i].priceLow)# to #lsCurrencyFormat(hotelResult.hotels[i].priceHigh)# - <a href="http://api.kayak.com#hotelResult.hotels[i].price.url#">Book It</a></cfoutput></p>
      <ul>
         <cfoutput>
         <li>Hotel: #hotelResult.hotels[i].name#    </li>
         <li>Stars: #hotelResult.hotels[i].stars#   </li>
         <li>Location: #hotelResult.hotels[i].city#, #hotelResult.hotels[i].region#, #hotelResult.hotels[i].country#</li>
         </cfoutput>
      </ul>
      </div>
      <hr/>
   </cfloop>
   <cfelse>
      <!--- If our search is not OK, then we get a msg back to explain the failure. --->
      <cfoutput>#results.msg#</cfoutput>
</cfif>
All samples are included in the download package. If you want to download the source, you can find it at my google code site.

Tags: Adobe · AJAX · ColdFusion · Flex · General · KayakAPI · Universal Mind · XML

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment