【问题标题】:PHP SoapClient target namespace not in request paramsPHP SoapClient 目标命名空间不在请求参数中
【发布时间】:2014-03-03 11:51:06
【问题描述】:

我在 WSDL 模式下使用 PHP SoapClient。

这是预期的 SOAP 请求应如下所示:

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns="http://xml.m4u.com.au/2009"> 
<soapenv:Header/> 
<soapenv:Body> 
 <ns:sendMessages> 
   <ns:authentication> 
     <ns:userId>Username</ns:userId> 
     <ns:password>Password</ns:password> 
   </ns:authentication> 
   <ns:requestBody> 
     <ns:messages> 
       <ns:message> 
         <ns:recipients> 
           <ns:recipient>61400000001</ns:recipient>  
         </ns:recipients> 
         <ns:content>Message Content</ns:content> 
       </ns:message> 
     </ns:messages> 
   </ns:requestBody> 
 </ns:sendMessages> 
</soapenv:Body> 
</soapenv:Envelope> 

这就是 PHP SoapClient 发送的内容:

<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://xml.m4u.com.au/2009">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns1:sendMessages>
  <ns1:authentication>
    <userId>Username</userId>
    <password>Password</password>
  </ns1:authentication>
  <ns1:requestBody>
    <messages>
      <message>
        <recipients>
          <recipient>61400000001</recipient>
        </recipients>
        <content>Message Content</content>
      </message>
    </messages>
  </ns1:requestBody>
</ns1:sendMessages>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这就是我构建客户端和参数的方式:

function sendMessages($recipient, $content) {

    $authenticationType = new SoapVar(array('userId' => $this->username, 'password' => $this->password), SOAP_ENC_OBJECT);

    $recipientsType = new SoapVar(array('recipient' => $recipient), SOAP_ENC_OBJECT);
    $messageType = new SoapVar(array('recipients' => $recipientsType, 'content' => $content), SOAP_ENC_OBJECT);
    $messagesType = new SoapVar(array('message' => $messageType), SOAP_ENC_OBJECT);

    $requestBodyType = new SoapVar(array('messages' => $messagesType), SOAP_ENC_OBJECT);

    $params = array(
                'authentication' => $authenticationType,
                'requestBody' => $requestBodyType
            );

    try {
        $this->soapClient = new SoapClient($this->wsdl, array('trace' => 1));
        $this->soapClient->__setSoapHeaders(array());

        return $this->soapClient->sendMessages($params);

    } catch (SoapFault $fault) {
        echo '<h2>Request</h2><pre>' . htmlspecialchars($this->soapClient->__getLastRequest(), ENT_QUOTES) . '</pre>';
        trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
    }

}

为什么 'authentication' 和 'requestBody' 存在 'ns1' 但它们的子节点却没有? 我究竟做错了什么? WSDL 位于此处 => http://soap.m4u.com.au/?wsdl

感谢任何可以提供帮助的人。

【问题讨论】:

    标签: php web-services soap wsdl


    【解决方案1】:

    您必须指定命名空间的 URI 才能对对象进行编码。这将包括所有必要的东西(包括您缺少的命名空间)。命名空间名称的首字母缩写无关。

    这个:

    $authenticationType = new SoapVar(array('userId' => $this->username, 'password' => $this->password), SOAP_ENC_OBJECT);
    

    应该是:

    $authenticationType = new SoapVar(array('userId' => $this->username, 'password' => $this->password), SOAP_ENC_OBJECT, "authentication","http://xml.m4u.com.au/2009");
    

    您在命名空间序列化中看到的问题来自于使用 soapvar 而没有所有参数。当它在发送之前序列化请求时,它假定命名空间已经包含在内。相反,如果您将每个都创建为一个简单的数组并将它们包含在$params 中,它将正确包含内部参数的命名空间。通过演示:

    $authenticationType = array('userId' => $this->username, 'password' => $this->password)
    

    【讨论】:

      【解决方案2】:

      尝试使用 nusoap 库。下面是示例代码:

      <?php
      $wsdl = "http://soap.m4u.com.au/?wsdl";
      // generate request veriables
      $data = array();
      
      $action = ""; // ws action
      $param = ""; //parameters
      
      $options = array(
                  'location'  => 'http://soap.m4u.com.au',
                  'uri'       => ''
                  );
      // eof generate request veriables
      
      //$client = new soap_client($wsdl, $options);// create soap client
      $client = new nusoap_client($wsdl, 'wsdl');
      
      $client->setCredentials($api_username, $api_password);// set crendencials
      $opt = $client->call($action, $param, '', '', false, true);
      
      print_r($opt);
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 2012-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多