uAPI-php-sample
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
<?php /* * uAPI sample communication in php language * * This example requires the cURL library to be installed and working. * * Please note, in the sample code below, the variable $CREDENTIALS is created by binding your username and password together with a colon e.g. * * $auth = base64_encode("Universal API/API1234567:mypassword"); * * The variable $TARGETBRANCH should be set to the TargetBranch provided by Travelport. * * (C) 2015 Travelport, Inc. * This code is for illustration purposes only. */ $TARGETBRANCH = 'Put your TargetBranch/WAB received in Welcome leter here'; $CREDENTIALS = 'Put your Username received in Welcome letter here:Put your password in Welcome letter here'; $Provider = '1G'; $message = <<<EOM <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header/> <soapenv:Body> <air:AvailabilitySearchReq TraceId="trace" AuthorizedBy="user" TargetBranch="$TARGETBRANCH" xmlns:air="http://www.travelport.com/schema/air_v29_0" xmlns:com="http://www.travelport.com/schema/common_v29_0"> <com:BillingPointOfSaleInfo OriginApplication="UAPI"/> <air:SearchAirLeg> <air:SearchOrigin> <com:Airport Code="DEN"/> </air:SearchOrigin> <air:SearchDestination> <com:Airport Code="SFO"/> </air:SearchDestination> <air:SearchDepTime PreferredTime="2015-06-27"> </air:SearchDepTime> </air:SearchAirLeg> <air:AirSearchModifiers MaxSolutions="1"> <air:PreferredProviders> <com:Provider Code="$Provider"/> </air:PreferredProviders> </air:AirSearchModifiers> </air:AvailabilitySearchReq> </soapenv:Body> </soapenv:Envelope> EOM; $file = "001-".$Provider."_AirAvailabilityReq.xml"; // file name to save the request xml for test only(if you want to save the request/response) prettyPrint($message,$file);//call function to pretty print xml $auth = base64_encode("$CREDENTIALS"); $soap_do = curl_init("https://apac.universal-api.pp.travelport.com/B2BGateway/connect/uAPI/AirService"); /*("https://americas.universal-api.pp.travelport.com/B2BGateway/connect/uAPI/AirService");*/ $header = array( "Content-Type: text/xml;charset=UTF-8", "Accept: gzip,deflate", "Cache-Control: no-cache", "Pragma: no-cache", "SOAPAction: \"\"", "Authorization: Basic $auth", "Content-length: ".strlen($message), ); //curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 30); //curl_setopt($soap_do, CURLOPT_TIMEOUT, 30); curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($soap_do, CURLOPT_POST, true ); curl_setopt($soap_do, CURLOPT_POSTFIELDS, $message); curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header); curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true); $return = curl_exec($soap_do); curl_close($soap_do); $file = "001-".$Provider."_AirAvailabilityRsp.xml"; // file name to save the response xml for test only(if you want to save the request/response) $content = prettyPrint($return,$file); parseOutput($content); //outputWriter($file, $return); //print_r(curl_getinfo($soap_do)); //Pretty print XML function prettyPrint($result,$file){ $dom = new DOMDocument; $dom->preserveWhiteSpace = false; $dom->loadXML($result); $dom->formatOutput = true; //call function to write request/response in file outputWriter($file,$dom->saveXML()); return $dom->saveXML(); } //function to write output in a file function outputWriter($file,$content){ file_put_contents($file, $content); // Write request/response and save them in the File } function parseOutput($content){ //parse the Search response to get values to use in detail request $AirAvailabilitySearchRsp = $content; //use this if response is not saved anywhere else use above variable //echo $AirAvailabilitySearchRsp; $xml = simplexml_load_String("$AirAvailabilitySearchRsp", null, null, 'SOAP', true); if($xml) echo "Processing! Please wait!"; else{ trigger_error("Encoding Error!", E_USER_ERROR); } $Results = $xml->children('SOAP',true); foreach($Results->children('SOAP',true) as $fault){ if(strcmp($fault->getName(),'Fault') == 0){ trigger_error("Error occurred request/response processing!", E_USER_ERROR); } } $count = 0; $fileName = "flights.txt"; if(file_exists($fileName)){ file_put_contents($fileName, ""); } foreach($Results->children('air',true) as $nodes){ foreach($nodes->children('air',true) as $hsr){ if(strcmp($hsr->getName(),'AirSegmentList') == 0){ foreach($hsr->children('air',true) as $hp){ if(strcmp($hp->getName(),'AirSegment') == 0){ $count = $count + 1; file_put_contents($fileName,"\r\n"."Air Segment ".$count."\r\n"."\r\n", FILE_APPEND); foreach($hp->attributes() as $a => $b ){ $GLOBALS[$a] = "$b"; //echo "$a"." : "."$b"; file_put_contents($fileName,$a." : ".$b."\r\n", FILE_APPEND); } } } } //break; } } $Token = 'Token'; $TokenKey = 'TokenKey'; $fileName = "tokens.txt"; if(file_exists($fileName)){ file_put_contents($fileName, ""); } foreach($Results->children('air',true) as $nodes){ foreach($nodes->children('air',true) as $hsr){ if(strcmp($hsr->getName(),'HostTokenList') == 0){ foreach($hsr->children('common_v29_0', true) as $ht){ if(strcmp($ht->getName(), 'HostToken') == 0){ $GLOBALS[$Token] = $ht[0]; foreach($ht->attributes() as $a => $b){ if(strcmp($a, 'Key') == 0){ file_put_contents($fileName,$TokenKey.":".$b."\r\n", FILE_APPEND); } } file_put_contents($fileName,$Token.":".$ht[0]."\r\n", FILE_APPEND); } } } } } echo "\r\n"."Processing Done. Please check results in files."; } ?> |