Как подключить WSDL API с помощью PHP - PullRequest
0 голосов
/ 11 октября 2018

Вот мой код, и я не могу понять, почему он не работает.

$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 голосов
/ 23 октября 2018

Я написал класс, который помогает упростить генерацию SOAP XML для пакета Laravel .Вот класс:

<?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. Не пытайтесь SOAPify или что-либо еще .Сохраните его в переменной $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 нет никаких гарантий, но попробуйтеи посмотри, получится ли это где-нибудь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...