dousaoxiancy199896 2016-08-14 18:34
浏览 42
已采纳

SOAP中的PHP纯XML

I am trying to call an API using SOAPclient but the authentification fail because namespace missing in the xml

Excepted XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://tourico.com/webservices/" xmlns:trav="http://tourico.com/travelservices/">
   <soapenv:Header>
      <web:LoginHeader>
         <trav:username>*****</trav:username>
         <trav:password>*******</trav:password>
         <trav:culture>en_US</trav:culture>
         <trav:version>7.123</trav:version>
      </web:LoginHeader>
   </soapenv:Header>
   <soapenv:Body>
      <web:CancelReservation>
         <web:nResID>1235456</web:nResID>
      </web:CancelReservation>
   </soapenv:Body>
</soapenv:Envelope>

What I am actualy sending (No namespace in the LoginHeader)

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tourico.com/webservices/" xmlns:ns2="http://tourico.com/travelservices/">
    <SOAP-ENV:Header>
        <ns2:LoginHeader>
            <username>******</username>
            <password>*******</password>
            <culture>en_US</culture>
            <version>8.0</version>
        </ns2:LoginHeader>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:CancelReservation>
            <ns1:nResID>95665639</ns1:nResID>
        </ns1:CancelReservation>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

My PHP code

    $url = "http://demo-wsnew.touricoholidays.com/ReservationsService.asmx?wsdl";

    $user = "*****";
    $pwd = "********";
    $culture = "en_US";
    $version = "8.0";

    $wsdl = $url;                  

    $client = new SOAPClient($wsdl,array("trace" => true, "exceptions" => true, 'soap_version' => SOAP_1_1));

    $login = new stdClass();
    $login->usernam= $user;
    $login->password = $pwd;
    $login->culture = $culture;
    $login->version = $version;


    // Turn auth header into a SOAP Header
    $header = new SoapHeader('http://tourico.com/travelservices', 'LoginHeader', $login, false);

    // set the header
    $client->__setSoapHeaders($header);


    $r = new stdClass();
    $r->nResID = 123456;

try
{
    $res = $client->CancelReservation($r);
    $results = json_decode(json_encode($res), true);
    Log::error($client->__getLastRequest());
    Log::error(print_r($results , true));
}
catch(Exception $e)
{
    Log::error($client->__getLastRequest());
    Log::error($e->getMessage());
}

Is there a way I could just send a XML string to the SOAPclient ?

  • 写回答

1条回答 默认 最新

  • dtpd58676 2016-09-19 20:31
    关注

    You were missing a trailing slash on the namespace, so that was likely part of the problem.

    This is a case where the classmap feature might help. Try this code:

    $url = "http://demo-wsnew.touricoholidays.com/ReservationsService.asmx?wsdl";
    
    $user = "*****";
    $pwd = "********";
    $culture = "en_US";
    $version = "8.0";
    
    $wsdl = $url;
    
    class MyLoginHeader
    {
      public $username;
      public $password;
      public $culture;
      public $version;
    }
    
    $classmap = array('LoginHeader' => 'MyLoginHeader');
    
    $client = new SOAPClient($wsdl,array("classmap" => $classmap,"trace" => true, "exceptions" => true, 'soap_version' => SOAP_1_1));
    
    $login = new MyLoginHeader();
    $login->username = $user;
    $login->password = $pwd;
    $login->culture = $culture;
    $login->version = $version;
    
    
    // Turn auth header into a SOAP Header
    $header = new SoapHeader('http://tourico.com/travelservices/', 'LoginHeader', $login, false);
    
    // set the header
    $client->__setSoapHeaders($header);
    
    
    $r = new stdClass();
    $r->nResID = 123456;
    
    try
    {
      $res = $client->CancelReservation($r);
      $results = json_decode(json_encode($res), true);
    }
    catch(Exception $e)
    {
      var_dump($client->__getLastRequest());
      var_dump($e->getMessage());
    }
    

    The code produces an XML request that looks like:

        <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:ns1="http://tourico.com/webservices/" xmlns:ns2="http://tourico.com/travelservices/"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <SOAP-ENV:Header>
            <ns2:LoginHeader xsi:type="ns2:LoginHeader">
                <ns2:username>*****</ns2:username>
                <ns2:password>********</ns2:password>
                <ns2:culture>en_US</ns2:culture>
                <ns2:version>8.0</ns2:version>
            </ns2:LoginHeader>
        </SOAP-ENV:Header>
        <SOAP-ENV:Body>
            <ns1:CancelReservation>
                <ns1:nResID>123456</ns1:nResID>
            </ns1:CancelReservation>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系