doujiao2443 2019-02-28 21:19
浏览 125
已采纳

Amadeus e-Power Web服务 - 发布设置SOAP身份验证标头

I'm having issues setting AuthenticationSoapHeader for Amadeus e-Power services. I'm using PHP's built in SoapClient.

WSDL: https://staging-ws.epower.amadeus.com/ws_usitcolours/EpowerService.asmx?WSDL

Basic class I've made to test things out:

class AmadeusSoapClient
{
    private $_client;
    private $_config = [
        'wsdl' => 'https://staging-ws.epower.amadeus.com/ws_usitcolours/EpowerService.asmx?WSDL',
        'namespace' => 'https://epowerv5.amadeus.com.tr/WS', 
        'auth' => [
            'username' => '...',
            'password' => '...'
        ], 
        'debug' => true
    ];

    // Construct
    public function __construct()
    {   
        // Client
        $this->_client = new \SoapClient($this->_config['wsdl'], [
            'trace' => $this->_config['debug']
        ]); 

        // Headers
        $headers = [ 
            // Auth
            new \SoapHeader($this->_config['namespace'], 'AuthenticationSoapHeader', [
                'WSUserName' => $this->_config['auth']['username'], 
                'WSPassword' => $this->_config['auth']['password']
            ]),     
        ]; 

        // Set headers 
        $this->_client->__setSoapHeaders($headers);
    }  

    // Request: CurrencyConversion
    public function currencyConversion($fromCurrency = 'BGN', $toCurrency = 'EUR', $amount = 1.00)
    { 
        $method = 'CurrencyConversion';
        $params = [
            'OTA_CurrencyConversionRQ' => [
                '_' => '',
                'FromCurrency' => $fromCurrency,
                'ToCurrency' => $toCurrency,
                'Amount' => $amount,
            ], 
        ];

        try { 
            return $this->_client->__call($method, [$params]);  
        } catch (\Exception $e) {
            // ...
        }  
    } 
}

Sample usage:

$client = new \AmadeusSoapClient();
$client->currencyConversion(); 

Error received:

stdClass Object
(
    [OTA_CurrencyConversionRS] => stdClass Object
        (
            [Errors] => stdClass Object
                (
                    [Error] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [_] => 
                                    [Type] => EpowerInternalError
                                    [ErrorCode] => EPW.0000
                                    [ShortText] => User Name is required
                                    [Code] => A001
                                    [NodeList] => EPower
                                    [BreakFlow] => 
                                )

                            [1] => stdClass Object
                                (
                                    [_] => 
                                    [Type] => EpowerInternalError
                                    [ErrorCode] => EPW.0000
                                    [ShortText] => Password is required
                                    [Code] => A001
                                    [NodeList] => EPower
                                    [BreakFlow] => 
                                )
                        )
                )
        )
)

No matter if I set headers with __setSopHeaders or pass them with the __call method, they are always ignored.. any other ideas how to set them?

Requests for methods that do not require Authentication are working properly.

Interesting thing is that the same request is working with Postman, but not with PHP SoapClient or SoapUI. Postman successful Soap request

  • 写回答

1条回答 默认 最新

  • douye2036 2019-03-23 18:05
    关注

    The problem was in the SSL communication between PHP's built in SoapClient and Amadeus's .NET server. Somehow the SSL connection could not be established, no matter how I set (force) Soap to use SSL I have always received the https connection must be used error.

    Sadly, I did not have more free time to investigate further and I decided to switch to cURL. Here is the simple class I made. Hope it helps someone!

    class AmadeusClient
    {
        private $_config = [
            'wsdl' => 'https://staging-ws.epower.amadeus.com/ws_usitcolours/EpowerService.asmx?WSDL',
            'namespace' => 'http://epowerv5.amadeus.com.tr/WS',
            'auth' => [
                'username' => '...',
                'password' => '...'
            ],
            'sessionCookieName' => 'ASP.NET_SessionId',
            'debug' => true
        ];
    
        public function testCall()
        {
            $fromCurrency = 'BGN';
            $toCurrency = 'USD';        
            $amount = 15.00;
            $result = $this->currencyConversionRequest($fromCurrency, $toCurrency, $amount);
            echo '<pre>';
            var_dump($result);
            exit; 
        }
    
        // Request: Currency conversion
        public function currencyConversionRequest($fromCurrency = 'BGN', $toCurrency = 'EUR', $amount = 1.00)
        {
            $method = 'CurrencyConversion';
            $params = [
                'OTA_CurrencyConversionRQ' => [
                    '_attributes' => [
                        'FromCurrency' => $fromCurrency,
                        'ToCurrency' => $toCurrency,
                        'Amount' => $amount,
                    ]
                ],
            ];
    
            $result = $this->sendRequest($method, $params);
            if (isset($result->Body->CurrencyConversionResponse->OTA_CurrencyConversionRS->Success)) {
                $array = (array) $result->Body->CurrencyConversionResponse->OTA_CurrencyConversionRS->attributes();
                if (!empty($array['@attributes'])) {
                    return $array['@attributes'];
                }
            }
            return false;
        } 
    
        public function sendRequest($method, $params)
        {
            $body = \Helpers\ArrayToXml::convert($params, [
                'rootElementName' => $method,
                '_attributes' => [
                    'xmlns' => $this->_config['namespace']
                ]
            ]);
            $requestBody = '
                <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                    <soap:Header>
                        <AuthenticationSoapHeader xmlns="'. $this->_config['namespace'] .'">
                            <WSUserName>'. $this->_config['auth']['username'] .'</WSUserName>
                            <WSPassword>'. $this->_config['auth']['password'] .'</WSPassword>
                        </AuthenticationSoapHeader>
                    </soap:Header>
                    <soap:Body>
                        '. str_replace('<?xml version="1.0"?>', '', $body) .'
                    </soap:Body>
                </soap:Envelope>
            '; 
    
            $headers = [
                'Content-type: text/xml; charset=utf-8',
                'Accept: text/xml',
                'Cache-Control: no-cache',
                'Pragma: no-cache',
                'SOAPAction: '. $this->_config['namespace'] .'/'. $method,
                'Content-length: '. mb_strlen($requestBody),
            ];
            $ch = curl_init($this->_config['wsdl']);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_VERBOSE, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            $output = curl_exec($ch);
            curl_close($ch);
    
            $cleanXml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $output);
            return simplexml_load_string($cleanXml);
        }
    }
    

    As of \Helpers\ArrayToXml, it is just a simple class by Spatie that converts PHP Array to XML. Source: https://github.com/spatie/array-to-xml

    Request XML generated by the above class:

    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Header>
            <AuthenticationSoapHeader xmlns="http://epowerv5.amadeus.com.tr/WS">
                <WSUserName>...</WSUserName>
                <WSPassword>...</WSPassword>
            </AuthenticationSoapHeader>
        </soap:Header>
        <soap:Body>
            <CurrencyConversion xmlns="http://epowerv5.amadeus.com.tr/WS">
                <OTA_CurrencyConversionRQ FromCurrency="BGN" ToCurrency="USD" Amount="15"/>
            </CurrencyConversion>
        </soap:Body>
    </soap:Envelope>
    

    Response:

    array(3) {
      ["Amount"]=>
      string(4) "9.00"
      ["TruncatedAmount"]=>
      string(4) "8.67"
      ["OtherChargesAmount"]=>
      string(4) "8.67"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)