如何使用PHP连接WSDL API

时间:2018-10-11 06:19:27

标签: php xml api soap

这是我的代码,我不知道为什么它不起作用。

$soapUrl = "http://airarabia.isaaviations.com/webservices/services/AAResWebServices?wsdl";

$xml_post_string = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Header><wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken wsu:Id="UsernameToken-17855236" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsse:Username>xxx</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">xxx</wsse:Password></wsse:UsernameToken></wsse:Security></soap:Header><soap:Body xmlns:ns2="http://www.opentravel.org/OTA/2003/05"><ns2:OTA_AirAvailRQ EchoToken="11868765275150-1300257933" PrimaryLangID="en-us" SequenceNmbr="1" Target="LIVE" TimeStamp="2018-10-08T11:39:35" Version="20061.00"><ns2:POS><ns2:Source TerminalID="Farhath/Farhath"><ns2:RequestorID ID="WSBENZTRAVELS" Type="4" /><ns2:BookingChannel Type="12" /></ns2:Source></ns2:POS><ns2:OriginDestinationInformation><ns2:DepartureDateTime>2018-10-30T00:00:00</ns2:DepartureDateTime><ns2:OriginLocation LocationCode="CMB" /><ns2:DestinationLocation LocationCode="RUH" /></ns2:OriginDestinationInformation><ns2:OriginDestinationInformation><ns2:DepartureDateTime>2018-11-30T00:00:00</ns2:DepartureDateTime><ns2:OriginLocation LocationCode="RUH" /><ns2:DestinationLocation LocationCode="CMB" /></ns2:OriginDestinationInformation><ns2:TravelerInfoSummary><ns2:AirTravelerAvail><ns2:PassengerTypeQuantity Code="ADT" Quantity="1" /></ns2:AirTravelerAvail></ns2:TravelerInfoSummary></ns2:OTA_AirAvailRQ></soap:Body></soap:Envelope>';

$headers = array(

"Host: airarabia.isaaviations.com",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($xml_post_string)
); 

$url = $soapUrl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch); 
curl_close($ch);

$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);

$parser = simplexml_load_string($response);

print_r($parser);

我能够连接到API,但无法获得结果。

您有什么想法,有解决方案吗?

1 个答案:

答案 0 :(得分:0)

我写了一个类,可以简化为Laravel package生成SOAP XML的过程。这是课程:

<?php

namespace Mtownsend\CollectionXml\Soap;

use SoapClient;

/**
 * Use the SoapFactory class to build a valid SOAP request
 * but prevent it from making an actual request
 * and capture the data it builds for use.
 */
class SoapFactory extends SoapClient
{
    public $soapRequest;
    public $soapLocation;
    public $soapAction;
    public $soapVersion;

    public function __construct($wsdl, $options)
    {
        parent::__construct($wsdl, $options);
    }

    /**
     * Build the SOAP xml string
     * @param  array  $soapRootAndXml [$soapRoot => $xml]
     * @return Mtownsend\CollectionXml\Soap\SoapFactory
     */
    public function build(array $soapRootAndXml)
    {
        $this->ProcessXMLRequest($soapRootAndXml);
        return $this;
    }

    /**
     * Override the SoapClient __doRequest method.
     */
    public function __doRequest($request, $location, $action, $version, $one_way = 0)
    {
        $this->soapRequest = $request;
        $this->soapLocation = $location;
        $this->soapAction = $action;
        $this->soapVersion = $version;
        return ''; // Return a string value or the SoapClient throws an exception
    }

    /**
     * A proxy for the getSoapRequest method.
     * @return string
     */
    public function getSoapXml()
    {
        return $this->getSoapRequest();
    }

    /**
     * Return the SOAP request XML.
     * @return string
     */
    public function getSoapRequest()
    {
        return $this->soapRequest;
    }

    /**
     * Return the SOAP request location url.
     * @return string
     */
    public function getSoapLocation()
    {
        return $this->soapLocation;
    }

    /**
     * Return the SOAP request action.
     * @return string
     */
    public function getSoapAction()
    {
        return $this->soapAction;
    }

    /**
     * Return the SOAP request version number.
     * @return string
     */
    public function getSoapVersion()
    {
        return $this->soapVersion;
    }
}

如果要使用它,请将内容保存到SoapFactory.php并将其包含在脚本中。

接下来,准备好原始xml。 请勿尝试对其进行SOAP化。将其存储在变量$xml_post_string中。

如果不起作用,则可能需要更改肥皂根。过去,我已经连接到使用'xmlBody'的肥皂端点。

$soapRoot = 'xmlBody';

$soapFactory = new SoapFactory($soapUrl, ['trace' => 1]));

$soapXml = $soapFactory->build([$soapRoot => $xml_post_string])->getSoapXml();

现在您可以尝试进行curl通话了。

$headers = array(

"Host: airarabia.isaaviations.com",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($soapXml)
); 

$url = $soapUrl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $soapXml);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch); 
curl_close($ch);

$parser = simplexml_load_string($response);

print_r($parser);

SOAP无法保证,但是请尝试一下,看看它能否带您到任何地方。