簡體   English   中英

php soap 客戶端如何更改命名空間前綴?

[英]php soap client How to change namespace prefix?

我想通過湯客戶端發送這樣的xml:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:hot="http://TekTravel/HotelBookingApi">
   <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
      <hot:Credentials UserName="test" Password="test@123"/>
      <wsa:Action>http://test/HotelBookingApi/CountryListRequest</wsa:Action>
      <wsa:To>http://api.test.in/HotelAPI_V7/HotelService.svc</wsa:To>
   </soap:Header>

   <soap:Body>
      <hot:CountryListRequest/>
   </soap:Body>
</soap:Envelope>

但它像這樣發送xml:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://TekTravel/HotelBookingApi">
    <env:Header>
        <ns1:Credentials UserName="test" Password="test@123"/>
        <ns1:Action>http://test/HotelBookingApi/HotelSearch</ns1:Action>
        <ns1:To>http://api.test.in/HotelAPI_V7/HotelService.svc</ns1:To>
    </env:Header>
    <env:Body>
        <ns1:CountryListRequest/>
    </env:Body>
</env:Envelope>

api 需要與第一個完全相同的 xml。我使用 Laravel 5.2,這是我的代碼:

<?php

namespace App\Model\Hotel;

use Illuminate\Database\Eloquent\Model;

use SoapClient;
use SoapHeader;
use SoapVar;

class HotelTBO extends Model
{

    //Credentials
    var $Credentials = ['UserName' => 'test', 'Password' => 'test@123'];

    //WSDL
    var $wsdl = 'http://api.tbotechnology.in/HotelAPI_V7/HotelService.svc?wsdl';

    //Actions
    var $ActionHotelSearch = "http://TekTravel/HotelBookingApi/HotelSearch";

    //NS
    var $NS = "http://TekTravel/HotelBookingApi";

    //TO
    var $ToHotelSearch = "http://api.tbotechnology.in/HotelAPI_V7/HotelService.svc";

    public function testSearch()
    {

        $client = new SoapClient(public_path('\assets\file\TBO.wsdl'), array('soap_version' => SOAP_1_2, 'trace' => 1, "exceptions" => 0));


        $HeaderVar = new SoapVar($this->HeaderXML, XSD_ANYXML, null, null, null);

        $Headers[] = new SoapHeader($this->NS, "Credentials", $this->Credentials,false);
        $Headers[] = new SoapHeader($this->NS, "Action", $this->ActionHotelSearch);
        $Headers[] = new SoapHeader($this->NS, "To", $this->ToHotelSearch);

        $client->__setSoapHeaders($Headers);
        $result = $client->__soapCall('CountryList', []);


    }

有一個名為https://github.com/BeSimple/BeSimpleSoap的包,但沒有任何相關文檔。 非常感謝您的幫助

僅使用 PHP soap 客戶端無法實現這一點。

使用 nusoap 必須配置參數

https://github.com/pwnlabs/nusoap

添加全局命名空間

$client->namespaces['tem'] = "http://tempuri.org/";

並為元素添加本地命名空間

 $dom  = new \DOMDocument('1.0');
            $document = $dom->createElement('tem:CAV_GET_DATA');
            $document = $dom->appendChild($document);
            $el = $dom->createElement("tem:UsuarioID", '111');
            $document->appendChild($el);
            $el = $dom->createElement("tem:Password", '111');
            $document->appendChild($el);
            $el = $dom->createElement("tem:idEmpleado", 111);
            $document->appendChild($el);
            $param = $dom->saveXML($dom->documentElement, LIBXML_NOEMPTYTAG);

示例完成

 $client = new nusoap_client('urlexamplee?WSDL', 'wsdl');
            $client->soap_defencoding = 'UTF-8';
            $client->response_timeout = 300
            $namespaces['tem'] = "http://tempuri.org/";

$dom  = new \DOMDocument('1.0');
                $document = $dom->createElement('tem:CAV_GET_DATA');
                $document = $dom->appendChild($document);
                $el = $dom->createElement("tem:UsuarioID", '111');
                $document->appendChild($el);
                $el = $dom->createElement("tem:Password", '111');
                $document->appendChild($el);
                $el = $dom->createElement("tem:idEmpleado", 111);
                $document->appendChild($el);
                $param = $dom->saveXML($dom->documentElement, LIBXML_NOEMPTYTAG);

 $responseService = $this->client->call(
                'CAV_GET_DATA',
                $param

            );

請求

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tem="http://tempuri.org/" xmlns:ns6704="tem"><SOAP-ENV:Body><tem:CAV_GET_DATA><tem:UsuarioID>example</tem:UsuarioID><tem:Password>example</tem:Password><tem:idEmpleado>000000</tem:idEmpleado></tem:CAV_GET_DATA></SOAP-ENV:Body></SOAP-ENV:Envelope>

發送請求時收到什么錯誤? 使用此代碼:

public function testSearch()
{

    $client = new SoapClient($this->wsdl, array('soap_version' => SOAP_1_2, 'trace' => 1, "exceptions" => 0));
    $cred_ns = 'http://TekTravel/HotelBookingApi';
    $action_ns = 'http://www.w3.org/2005/08/addressing';

    $credentialHeader = ['UserName' => 'test', 'Password' => 'test@123'];
    $actionHeader = 'http://TekTravel/HotelBookingApi/CountryList';
    $toHeader = 'http://api.test.in/HotelAPI_V7/HotelService.svc';

    $Headers[] = new SoapHeader($cred_ns, 'Credentials', $credentialHeader , false);
    $Headers[] = new SoapHeader($action_ns, "Action", $actionHeader, false);
    $Headers[] = new SoapHeader($action_ns, "To", $toHeader, false);

    $client->__setSoapHeaders($Headers);
    $result = $client->__soapCall('CountryList', []);
    print_r($client->__getLastRequest());
    echo PHP_EOL;
    print_r($result);
}

我收到如下請求:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://TekTravel/HotelBookingApi" xmlns:ns2="http://www.w3.org/2005/08/addressing">
   <env:Header>
      <ns1:Credentials UserName="test" Password="test@123"/>
      <ns2:Action>http://TekTravel/HotelBookingApi/CountryList</ns2:Action>
      <ns2:To>http://api.test.in/HotelAPI_V7/HotelService.svc</ns2:To>
    </env:Header>
    <env:Body>
        <ns1:CountryListRequest/>
    </env:Body>
</env:Envelope>

結果是授權錯誤:stdClass Object

(
[Status] => stdClass Object
    (
        [StatusCode] => 02
        [Description] => LoginErr: Login Failed.
    )
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM