php - SOAP :信封 SOAP-ENV:信封 PHP

标签 php xml soap soap-client

我正在尝试使用 PHP 的内置 soap 函数登录 API。我得到了这样的结果。

[LoginResult]=> false,
[ErrorMsg] => Login failed with the reason : The security object is invalid

这是 API 提供者的要求。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
    <Login xmlns="http://tempuri.org/Example/Service1">
          <objSecurity>
              <WebProviderLoginId>test</WebProviderLoginId>
              <WebProviderPassword>test</WebProviderPassword>
              <IsAgent>false</IsAgent>
          </objSecurity>
          <OutPut />
          <ErrorMsg />
    </Login>
</soap:Body>
</soap:Envelope>

&,这是我能够使用函数生成的内容。

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/Example/Service1">
<SOAP-ENV:Body>
    <ns1:Login>
        <objSecurity>
             <WebProviderLoginId>test</WebProviderLoginId>
             <WebProviderPassword>test</WebProviderPassword>
             <IsAgent>false</IsAgent>
        </objSecurity>
        <OutPut/>
        <ErrorMsg/>
    </ns1:Login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是我用来发送请求的代码。

<?php
class objSecurity {
function objSecurity($s, $i, $f)   {
    $this->WebProviderLoginId = $s;
    $this->WebProviderPassword = $i;
    $this->IsAgent = $f;
}
}

class nextObject {
function nextObject($objSecurity)   {
    $this->objSecurity=$pobjSecurity;
    $this->OutPut=NULL;
    $this->ErrorMsg=NULL;
}
}

$url    = 'http://example.com/sampleapi/test.asmx?WSDL';
$client = new SoapClient($url, array("soap_version" => SOAP_1_1,"trace" => 1));
$struct = new objSecurity('test', 'test', false);
$data   = new nextObject($struct);
$soapstruct2 = new SoapVar($data, SOAP_ENC_OBJECT);
print_r(
   $client->__soapCall(
       "Login",
       array(new SoapParam($soapstruct2, "inputStruct"))
   )
);

echo $client->__getLastRequest();

?>

这些是我发现的差异。

在我的请求中 xmlns:xsi丢失了。

要求以<soap:Envelope开头, 但是我的请求以<SOAP-ENV:Envelope开头.

还有一个额外的xmlns:ns1在我的要求下。

& 函数名标签以ns1:开头.

请帮助我将我的请求转换成要求的格式。

我对 SOAP 了解不多,我使用的是 PHP 5.3.13 版和 CakePHP 2.3.0。对不起,我的英语不好。

最佳答案

这是解决方案。 :)

<?php
$url    = 'http://example.com/sampleapi/test.asmx?WSDL';
$client = new SoapClient($url, array("soap_version" => SOAP_1_1,"trace" => 1));

$user_param = array (
  'WebProviderLoginId' => "test",
  'WebProviderPassword' => "test",
  'IsAgent' => false
);

$service_param = array (
  'objSecurity' => $user_param,
  "OutPut" => NULL,
  "ErrorMsg" => NULL
);

print_r(
   $client->__soapCall(
       "Login",
       array($service_param)
   )
);

echo $client->__getLastRequest();

?>

&请求是:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/Example/Service1">
<SOAP-ENV:Body>
   <ns1:Login>
       <ns1:objSecurity>
           <ns1:WebProviderLoginId>test</ns1:WebProviderLoginId>
           <ns1:WebProviderPassword>test</ns1:WebProviderPassword>
           <ns1:IsAgent>false</ns1:IsAgent>
       </ns1:objSecurity>
   </ns1:Login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

感谢这个链接。 PHP SOAP Request not right

关于php - SOAP :信封 SOAP-ENV:信封 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14770898/

相关文章:

php - 登录系统sqlserver连接错误

python - 使用python重复查询xml

android swipelist 缺少属性

c# - 在没有单独方法的情况下在 RESTful WCF 中混合 XML 和 JSON

php - Codeigniter 弃用 : mysql_real_escape_string():

php - 一条update语句更新多个mysql表出错

node.js - node-soap 只向 Mechanical Turk 发送 "Help"请求?

java - 序列化整数[]

php - 将两个数组合并或组合成单个数组

r - 在 R 中,如果我有 Web 服务的 WSDL 描述,我该如何调用它? (消费网络服务)