使用curl的Amadeus Flight API实现

时间:2018-12-11 22:32:58

标签: curl amadeus

我想通过curl使用Amadeus Flight API。因此,请帮助我使用核心PHP来实现api。如果您可以为我提供php中rest api的所有三个示例(获取,发布,放置,删除),那就太好了。

3 个答案:

答案 0 :(得分:0)

这是带有curl进行授权的php的示例(因此为POST)。要获取您的 API密钥 API秘密,您可以按照Get Started Guide

$url = 'https://test.api.amadeus.com/v1/security/oauth2/token';
$curls = curl_init();
curl_setopt($curls, CURLOPT_URL, $url);
curl_setopt($curls, CURLOPT_POST, true);
curl_setopt($curls, CURLOPT_POSTFIELDS, 'grant_type=client_credentials&client_id=API_KEY&client_secret=API_SECRET');
curl_setopt($curls, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$token = curl_exec($curls);
curl_close($curls);
print_r ($token);

您将需要从上一个调用中获取access_token并将其添加到下一个调用的头中。这是一个调用Flight Inspiration API的示例(请不要忘记替换 access_token ):

$url = 'https://test.api.amadeus.com/v1/shopping/flight-destinations?origin=MAD';
$curls = curl_init();
curl_setopt($curls, CURLOPT_URL, $url);

curl_setopt($curls, CURLOPT_HTTPHEADER, array('Authorization: Bearer access_token'));
$result = curl_exec($curls);
    if (curl_errno($curls)) {
        echo 'Error:' . curl_error($curls);
    }
print_r ($result);
curl_close ($curls);

如果您想要更多示例,请查看Postman collection,将语言切换为PHP,并在右侧面板上获得示例(不要忘记添加标题):

  

CURLOPT_HTTPHEADER => array('Authorization:Bearer access_token'),

答案 1 :(得分:0)

我的员工(dahabtours GmbH)同意发布我们的API包装器类,以便将某些东西还给开源社区,因为还没有官方的PHP SDK。这使得在PHP中使用自助服务API变得容易得多。

https://github.com/dahabtours/amadeus-php-sdk

$amadeus_api = new AmadeusDahabtours\SelfServiceApiClient({CLIENT_ID}, {CLIENT_SECRET});

$amadeus_results = $amadeus_api->lowFares([
    'origin'        => 'FRA',
    'destination'   => 'CAI',
    'departureDate' => '2019-07-15',
]);

答案 2 :(得分:0)

此代码基于PHP Curl,并且可以在所有类型的API环境中使用:

$wsdl='.....';  


$nonceBase=generateSomewhatRandomString();

$userId='XXXXXXXX';

$officeId='XXXXXXXXX';

$Pass=base64_encode('XXXXXXXX');

$messageId=generateGuid();

 $password = base64_decode($Pass);

            $creation = new \DateTime('now', new \DateTimeZone('UTC'));

            $t = microtime(true);

            $micro = sprintf("%03d", ($t - floor($t)) * 1000);

            $creationString = createDateTimeStringForAuth($creation, $micro);

            $messageNonce = generateUniqueNonce($nonceBase, $creationString);

            $encodedNonce = base64_encode($messageNonce);

            $digest = generatePasswordDigest($password, $creationString, $messageNonce);

 $SOAP_API_URL='https://nodeD2.test.webservices.amadeus.com/XXXXXXXXXXX';

$action='http://webservices.amadeus.com/XXXXXXXXXX';

$actionLast='XXXXXXXXXX';   

            $soapXML='<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://xml.amadeus.com/'.$actionLast.'" xmlns:ns2="http://www.w3.org/2005/08/addressing" xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-secext-1.0.xsd" xmlns:ns5="http://xml.amadeus.com/2010/06/Security_v1"><SOAP-ENV:Header><ns2:MessageID>'.$messageId.'</ns2:MessageID><ns2:Action>'.$action.'</ns2:Action><ns2:To>https://nodeD2.test.webservices.amadeus.com/1ASIWQTC1QI</ns2:To><oas:Security xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><oas:UsernameToken xmlns:oas1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" Id="UsernameToken-1"><oas:Username>'.$userId.'</oas:Username><oas:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">'.$encodedNonce.'</oas:Nonce><oas:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">'.$digest.'</oas:Password><oas1:Created>'.$creationString.'</oas1:Created></oas:UsernameToken></oas:Security><ns5:AMA_SecurityHostedUser><ns5:UserID POS_Type="1" PseudoCityCode="'.$officeId.'" AgentDutyCode="SU" RequestorType="U"/></ns5:AMA_SecurityHostedUser></SOAP-ENV:Header><SOAP-ENV:Body>'.$wsdl.'</SOAP-ENV:Body></SOAP-ENV:Envelope>
';

$soapUser=$userId;

$soapPassword=$Pass;

$headers = [
        "Content-type: application/xml",
        'SOAPAction: "' . $action . '"'
    ];
     $ch = curl_init();

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_URL,$SOAP_API_URL);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $soapXML);
    $data = curl_exec($ch);
$your_xml_response = html_entity_decode($data);
$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:', 'awsse:', 'wsa:'], '',$your_xml_response);
$clean_xml = str_ireplace(['&'], '&amp;', $clean_xml);
$Results = simplexml_load_string($clean_xml);