{"id":318,"date":"2018-08-29T14:16:55","date_gmt":"2018-08-29T05:16:55","guid":{"rendered":"https:\/\/uapi.ihavenomoney.co.kr\/?p=318"},"modified":"2018-08-29T14:16:55","modified_gmt":"2018-08-29T05:16:55","slug":"uapi-php-sample","status":"publish","type":"post","link":"https:\/\/uapi.ihavenomoney.co.kr\/?p=318","title":{"rendered":"uAPI-php-sample"},"content":{"rendered":"\n<pre class=\"lang:php decode:true \" >&lt;?php \r\n\/* \r\n* uAPI sample communication in php language \r\n* \r\n* This example requires the cURL library to be installed and working. \r\n* \r\n* Please note, in the sample code below, the variable $CREDENTIALS is created by binding your username and password together with a colon e.g. \r\n* \r\n* $auth = base64_encode(\"Universal API\/API1234567:mypassword\"); \r\n* \r\n* The variable $TARGETBRANCH should be set to the TargetBranch provided by Travelport. \r\n* \r\n* (C) 2015 Travelport, Inc. \r\n* This code is for illustration purposes only. \r\n*\/\r\n$TARGETBRANCH = 'Put your TargetBranch\/WAB received in Welcome leter here';\r\n$CREDENTIALS = 'Put your Username received in Welcome letter here:Put your password in Welcome letter here'; \r\n$Provider = '1G';\r\n$message = &lt;&lt;&lt;EOM\r\n&lt;soapenv:Envelope xmlns:soapenv=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\"&gt;\r\n   &lt;soapenv:Header\/&gt;\r\n   &lt;soapenv:Body&gt;\r\n      &lt;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\"&gt;\r\n         &lt;com:BillingPointOfSaleInfo OriginApplication=\"UAPI\"\/&gt;\r\n         &lt;air:SearchAirLeg&gt;\r\n            &lt;air:SearchOrigin&gt;\r\n               &lt;com:Airport Code=\"DEN\"\/&gt;\r\n            &lt;\/air:SearchOrigin&gt;\r\n            &lt;air:SearchDestination&gt;\r\n               &lt;com:Airport Code=\"SFO\"\/&gt;\r\n            &lt;\/air:SearchDestination&gt;\r\n            &lt;air:SearchDepTime PreferredTime=\"2015-06-27\"&gt;\r\n            &lt;\/air:SearchDepTime&gt;            \r\n         &lt;\/air:SearchAirLeg&gt;\r\n         &lt;air:AirSearchModifiers MaxSolutions=\"1\"&gt;\r\n            &lt;air:PreferredProviders&gt;\r\n               &lt;com:Provider Code=\"$Provider\"\/&gt;\r\n            &lt;\/air:PreferredProviders&gt;\r\n         &lt;\/air:AirSearchModifiers&gt;\r\n      &lt;\/air:AvailabilitySearchReq&gt;\r\n   &lt;\/soapenv:Body&gt;\r\n&lt;\/soapenv:Envelope&gt;\r\nEOM;\r\n\r\n\r\n$file = \"001-\".$Provider.\"_AirAvailabilityReq.xml\"; \/\/ file name to save the request xml for test only(if you want to save the request\/response)\r\nprettyPrint($message,$file);\/\/call function to pretty print xml\r\n\r\n\r\n$auth = base64_encode(\"$CREDENTIALS\"); \r\n$soap_do = curl_init(\"https:\/\/apac.universal-api.pp.travelport.com\/B2BGateway\/connect\/uAPI\/AirService\");\r\n\/*(\"https:\/\/americas.universal-api.pp.travelport.com\/B2BGateway\/connect\/uAPI\/AirService\");*\/\r\n$header = array(\r\n\"Content-Type: text\/xml;charset=UTF-8\", \r\n\"Accept: gzip,deflate\", \r\n\"Cache-Control: no-cache\", \r\n\"Pragma: no-cache\", \r\n\"SOAPAction: \\\"\\\"\",\r\n\"Authorization: Basic $auth\", \r\n\"Content-length: \".strlen($message),\r\n); \r\n\/\/curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 30); \r\n\/\/curl_setopt($soap_do, CURLOPT_TIMEOUT, 30); \r\ncurl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false); \r\ncurl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); \r\ncurl_setopt($soap_do, CURLOPT_POST, true ); \r\ncurl_setopt($soap_do, CURLOPT_POSTFIELDS, $message); \r\ncurl_setopt($soap_do, CURLOPT_HTTPHEADER, $header); \r\ncurl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);\r\n$return = curl_exec($soap_do);\r\ncurl_close($soap_do);\r\n\r\n$file = \"001-\".$Provider.\"_AirAvailabilityRsp.xml\"; \/\/ file name to save the response xml for test only(if you want to save the request\/response)\r\n$content = prettyPrint($return,$file);\r\nparseOutput($content);\r\n\/\/outputWriter($file, $return);\r\n\/\/print_r(curl_getinfo($soap_do));\r\n\r\n\r\n\r\n\r\n\/\/Pretty print XML\r\nfunction prettyPrint($result,$file){\r\n    $dom = new DOMDocument;\r\n    $dom-&gt;preserveWhiteSpace = false;\r\n    $dom-&gt;loadXML($result);\r\n    $dom-&gt;formatOutput = true;      \r\n    \/\/call function to write request\/response in file   \r\n    outputWriter($file,$dom-&gt;saveXML());    \r\n    return $dom-&gt;saveXML();\r\n}\r\n\r\n\/\/function to write output in a file\r\nfunction outputWriter($file,$content){  \r\n    file_put_contents($file, $content); \/\/ Write request\/response and save them in the File\r\n}\r\n\r\n\r\nfunction parseOutput($content){ \/\/parse the Search response to get values to use in detail request\r\n    $AirAvailabilitySearchRsp = $content; \/\/use this if response is not saved anywhere else use above variable\r\n    \/\/echo $AirAvailabilitySearchRsp;\r\n    $xml = simplexml_load_String(\"$AirAvailabilitySearchRsp\", null, null, 'SOAP', true);    \r\n\r\n    if($xml)\r\n        echo \"Processing! Please wait!\";\r\n    else{\r\n        trigger_error(\"Encoding Error!\", E_USER_ERROR);\r\n    }\r\n\r\n    $Results = $xml-&gt;children('SOAP',true);\r\n    foreach($Results-&gt;children('SOAP',true) as $fault){\r\n        if(strcmp($fault-&gt;getName(),'Fault') == 0){\r\n            trigger_error(\"Error occurred request\/response processing!\", E_USER_ERROR);\r\n        }\r\n    }\r\n\r\n    $count = 0;\r\n    $fileName = \"flights.txt\";\r\n    if(file_exists($fileName)){\r\n        file_put_contents($fileName, \"\");\r\n    }\r\n    foreach($Results-&gt;children('air',true) as $nodes){\r\n        foreach($nodes-&gt;children('air',true) as $hsr){\r\n            if(strcmp($hsr-&gt;getName(),'AirSegmentList') == 0){\r\n                foreach($hsr-&gt;children('air',true) as $hp){\r\n                    if(strcmp($hp-&gt;getName(),'AirSegment') == 0){\r\n                        $count = $count + 1;\r\n                        file_put_contents($fileName,\"\\r\\n\".\"Air Segment \".$count.\"\\r\\n\".\"\\r\\n\", FILE_APPEND);\r\n                        foreach($hp-&gt;attributes() as $a =&gt; $b   ){\r\n                                $GLOBALS[$a] = \"$b\";\r\n                                \/\/echo \"$a\".\" : \".\"$b\";\r\n                                file_put_contents($fileName,$a.\" : \".$b.\"\\r\\n\", FILE_APPEND);\r\n                        }                                               \r\n                    }                   \r\n                }\r\n            }\r\n            \/\/break;\r\n        }\r\n    }\r\n    $Token = 'Token';\r\n    $TokenKey = 'TokenKey';\r\n    $fileName = \"tokens.txt\";\r\n    if(file_exists($fileName)){\r\n        file_put_contents($fileName, \"\");\r\n    }\r\n    foreach($Results-&gt;children('air',true) as $nodes){\r\n        foreach($nodes-&gt;children('air',true) as $hsr){\r\n            if(strcmp($hsr-&gt;getName(),'HostTokenList') == 0){           \r\n                foreach($hsr-&gt;children('common_v29_0', true) as $ht){\r\n                    if(strcmp($ht-&gt;getName(), 'HostToken') == 0){\r\n                        $GLOBALS[$Token] = $ht[0];\r\n                        foreach($ht-&gt;attributes() as $a =&gt; $b){\r\n                            if(strcmp($a, 'Key') == 0){\r\n                                file_put_contents($fileName,$TokenKey.\":\".$b.\"\\r\\n\", FILE_APPEND);\r\n                            }\r\n                        }                       \r\n                        file_put_contents($fileName,$Token.\":\".$ht[0].\"\\r\\n\", FILE_APPEND);\r\n                    }\r\n                }\r\n            }\r\n        }\r\n    }\r\n\r\n    echo \"\\r\\n\".\"Processing Done. Please check results in files.\";\r\n\r\n}\r\n\r\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>&lt;?php \/* * uAPI sample communication in php language * * This example requires the cURL library to be installed and working. * * Please <a class=\"mh-excerpt-more\" href=\"https:\/\/uapi.ihavenomoney.co.kr\/?p=318\" title=\"uAPI-php-sample\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-318","post","type-post","status-publish","format-standard","hentry","category-util"],"_links":{"self":[{"href":"https:\/\/uapi.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/uapi.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/uapi.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/uapi.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/uapi.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318"}],"version-history":[{"count":1,"href":"https:\/\/uapi.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":319,"href":"https:\/\/uapi.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions\/319"}],"wp:attachment":[{"href":"https:\/\/uapi.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/uapi.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/uapi.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}