dqzlqfqeh845799833 2018-10-11 06:19 采纳率: 0%
浏览 40
已采纳

如何使用php连接wsdl api

Here is my code, and I can't figure out why it's not working.

$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);

I am able to connect to the API, but couldn't get a result.

Do you have any idea, any solution ?

  • 写回答

1条回答 默认 最新

  • dowjgrm6787 2018-10-23 15:44
    关注

    I wrote a class that helps to simplify generating SOAP XML for a Laravel package. Here is the class:

    <?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;
        }
    }
    

    If you would like to use it, save the contents to SoapFactory.php and include it in your script.

    Next, get your raw xml ready. Don't try to SOAPify it or anything. Store it in a variable $xml_post_string.

    You may need to change your soap root if it doesn't work. I've connected to soap endpoints in the past that use 'xmlBody'.

    $soapRoot = 'xmlBody';
    
    $soapFactory = new SoapFactory($soapUrl, ['trace' => 1]));
    
    $soapXml = $soapFactory->build([$soapRoot => $xml_post_string])->getSoapXml();
    

    Now you can try a curl call.

    $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);
    

    There's no guarantee with SOAP, but try that and see if it gets you anywhere.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决