我想通过 curl 使用 Amadeus Flight API。所以帮助我使用核心 PHP 实现 api。如果您可以为我提供 php 中其余 api 的所有三个示例(获取、发布、放置、删除),那就太好了。
问问题
963 次
3 回答
1
我的雇主(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',
]);
于 2019-02-20T12:14:41.247 回答
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(['&'], '&', $clean_xml);
$Results = simplexml_load_string($clean_xml);
于 2020-05-22T05:28:55.553 回答
0
这是一个使用 curl 进行授权的 php 示例(因此是 POST)。要获取您的API 密钥和API密钥,您可以按照入门指南进行操作
$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 集合,将语言切换为 PHP 并在右侧面板中获取示例(不要忘记添加标题):
CURLOPT_HTTPHEADER => array('Authorization: Bearer access_token'),
于 2018-12-12T00:01:32.010 回答