与酒店的PHP Sabre Soap API集成

时间:2016-03-17 07:15:36

标签: php curl soap sabre

从此处遵循Sabre身份验证过程

https://developer.sabre.com/docs/read/soap_basics/Authentication

想用php获取saber SOAP API结果,但是使用curl获取响应有问题,如下面的代码所示

$input_xml = '
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:eb="http://www.ebxml.org/namespaces/messageHeader" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsd="http://www.w3.org/1999/XMLSchema">
    <SOAP-ENV:Header>
        <eb:MessageHeader SOAP-ENV:mustUnderstand="1" eb:version="1.0">
            <eb:ConversationId/>
            <eb:From>
                <eb:PartyId type="urn:x12.org:IO5:01">999999</eb:PartyId>
            </eb:From>
            <eb:To>
                <eb:PartyId type="urn:x12.org:IO5:01">123123</eb:PartyId>
            </eb:To>
            <eb:CPAId>IPCC</eb:CPAId>
            <eb:Service eb:type="OTA">SessionCreateRQ</eb:Service>
            <eb:Action>SessionCreateRQ</eb:Action>
            <eb:MessageData>
                <eb:MessageId>1000</eb:MessageId>
                <eb:Timestamp>2001-02-15T11:15:12Z</eb:Timestamp>
                <eb:TimeToLive>2001-02-15T11:15:12Z</eb:TimeToLive>
            </eb:MessageData>
        </eb:MessageHeader>
        <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/12/utility">
            <wsse:UsernameToken> 
                <wsse:Username>USERNAME</wsse:Username>
                <wsse:Password>PASSWORD</wsse:Password>
                <Organization>IPCC</Organization>
                <Domain>DEFAULT</Domain> 
            </wsse:UsernameToken>
        </wsse:Security>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <eb:Manifest SOAP-ENV:mustUnderstand="1" eb:version="1.0">
            <eb:Reference xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="cid:rootelement" xlink:type="simple"/>
        </eb:Manifest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
    $url = $envUrl;

        //setting the curl parameters.
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
// Following line is compulsary to add as it is:
        curl_setopt($ch, CURLOPT_POSTFIELDS,
                    "xmlRequest=" . $input_xml);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
        $data = curl_exec($ch);
        curl_close($ch);

        //convert the XML result into array
        $array_data = json_decode(json_encode(simplexml_load_string($data)), true);

        print_r('<pre>');
        print_r($array_data);
        print_r('</pre>');

$array_data没有返回任何内容+也尝试在

之前创建会话

https://developer.sabre.com/docs/read/soap_apis/session_management/create_session

但反应相同。我知道在php中有一些与saber通信的好方法,请帮我找到它

1 个答案:

答案 0 :(得分:1)

这个问题已经有几个月了,但也许我的回答可能仍然有用。

我成功地使用PHP和CURL成功地与Sabre的SOAP API进行通信。看看你的代码并与我自己的代码进行比较,我有一些建议:

1)尝试使用SOAP调用传递一些HTTP头信息,如下所示(我记得这有点重要):

$action = 'SessionCreateRQ'; // Set this to whatever Sabre API action you are calling

$soapXML = $input_xml; // Your SOAP XML

$headers = array( 
   'Content-Type: text/xml; charset="utf-8"', 
   'Content-Length: ' . strlen($soapXML), 
   'Accept: text/xml', 
   'Keep-Alive: 300', 
   'Connection: keep-alive', 
   'Cache-Control: no-cache', 
   'Pragma: no-cache', 
   'SOAPAction: "' . $action . '"'
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $soapXML);

2)在调用curl_exec之后,检查以下任何有助于调试的错误:

$data = curl_exec($ch);
if(curl_error($ch)) {
    echo "Curl error: " . curl_error($ch);
} else {
    echo $data;
    ...
}

3)许多Sabre SOAP API调用要求您首先创建会话。因此,通常需要使用SessionCreateRQ发出SOAP请求,之后您可以为所需的操作进行另一个SOAP调用,然后再向SessionCloseRQ发出另一个SOAP请求以关闭会话。每个SOAP请求都需要正确设置完整的头部和有效负载,这可能有点痛苦,但这是所需的流程。

我希望有所帮助!