SOAP请求PHP5

时间:2010-05-03 19:12:22

标签: php web-services soap

我正在努力提出肥皂要求并遇到困难。

这是我的php:

<?php
$option=array('trace'=>1);
$soapClient = new SoapClient('http://www.domain.com/ws/AccountManagement.wsdl', $option); 

$headers = array('LOGIN_ID' => 'user@user.com', 'LOGIN_PASSWORD' => 'mypassword'); 

$header = new SoapHeader('https://www.domain.com', 'AuthenticationDTO', $headers, false);  

$soapClient->__setSoapHeaders($header); //or array($header)

$params = array(
'bureauName' => '',
'businessInformation' => array('address' => array('city' => 'SomeCity'), array('country' => 'US'), array('state' => 'MN'), array('street' => 'Some Address'), array('zipCode' => '33212')), array('businessName' => 'SomeBusinessName'),
'entityType' => '',
'listOfSimilars' => 'true',
);


try { 
    $result = $soapClient->__call("matchCompany", $params);
    print_r($result);
} catch (SoapFault $fault) { 
   echo $fault->faultcode . "-" . $fault->faultstring; 
   echo "REQUEST:\n" . htmlentities($soapClient->__getLastRequest()) . "\n";
} 

&GT;

$ soapClient__call失败。

getLastRequest()生成此XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://com.dnbi.eai.service.AccountSEI" xmlns:ns2="http://dto.eai.dnbi.com">
  <SOAP-ENV:Header>
    <ns2:AuthenticationDTO>
      <ns2:LOGIN_ID>user@user.com</ns2:LOGIN_ID>
      <ns2:LOGIN_PASSWORD>mypassword</ns2:LOGIN_PASSWORD>
    </ns2:AuthenticationDTO>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns1:matchCompany/>
    <param1><item>
      <key>address</key>
      <value><item>
        <key>city</key>
        <value>SomeCity</value>
        </item></value>
      </item><item>
      <key>0</key>
      <value><item>
        <key>country</key>
        <value>US</value>
        </item></value>
      </item><item>
      <key>1</key>
      <value><item>
        <key>state</key>
        <value>MN</value>
        </item></value>
      </item><item>
      <key>2</key>
      <value><item>
        <key>street</key>
        <value>Some Address</value>
        </item></value>
      </item><item>
      <key>3</key>
      <value><item>
        <key>zipCode</key>
        <value>33212</value>
        </item></value>
      </item></param1>
    <param2><item>
      <key>businessName</key>
      <value>SomeBusinessName</value>
      </item></param2>
    <param3></param3>
    <param4>true</param4>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这不是正确的XML输出。我一定做错了什么。使用soapUI我发现这是正确的XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dto="http://dto.eai.dnbi.com" xmlns:com="http://com.dnbi.eai.service.AccountSEI">
   <soapenv:Header>
      <dto:AuthenticationDTO>
         <dto:LOGIN_ID>user@user.com</dto:LOGIN_ID>
         <dto:LOGIN_PASSWORD>mypassword</dto:LOGIN_PASSWORD>
      </dto:AuthenticationDTO>
   </soapenv:Header>
   <soapenv:Body>
      <com:matchCompany>
         <com:in0>
            <!--Optional:-->
            <dto:bureauName></dto:bureauName>
            <!--Optional:-->
            <dto:businessInformation>
               <dto:address>
         <!--Optional:-->
                  <dto:city>SomeCity</dto:city>
                  <dto:country>US</dto:country>
                  <dto:state>MN</dto:state>
                  <dto:street>Some Address</dto:street>
                  <!--Optional:-->
                  <dto:zipCode></dto:zipCode>
               </dto:address>
               <!--Optional:-->
               <dto:businessName>SomeBusinessName</dto:businessName>
            </dto:businessInformation>
            <!--Optional:-->
            <dto:entityNumber></dto:entityNumber>
            <!--Optional:-->
            <dto:entityType></dto:entityType>
            <!--Optional:-->
            <dto:listOfSimilars>true</dto:listOfSimilars>
         </com:in0>
      </com:matchCompany>
   </soapenv:Body>
</soapenv:Envelope>

有人可以帮我生成soapUI为我生成的相同XML,但是使用PHP5的原生Soap客户端吗?

由于

1 个答案:

答案 0 :(得分:4)

请尝试使用此代码。请注意它如何使用匿名对象来创建所需的层次结构:

$options = array(
    'trace' => 1
);

$soapClient = new SoapClient('http://www.domain.com/ws/AccountManagement.wsdl', $option);  

$headers = array(
    'LOGIN_ID' => 'user@user.com',
    'LOGIN_PASSWORD' => 'mypassword'
);  

$header = new SoapHeader('dto', 'AuthenticationDTO', $headers, false);   

$soapClient->__setSoapHeaders($header); 

$matchCompany->in0->bureauName = '';
$matchCompany->in0->businessInformation->address->city = 'SomeCity';
$matchCompany->in0->businessInformation->address->country = 'US';
$matchCompany->in0->businessInformation->address->state = 'MN';
$matchCompany->in0->businessInformation->address->street = 'Some Address';
$matchCompany->in0->businessInformation->address->zipCode = '33212';
$matchCompany->in0->businessInformation->businessName = 'SomeBusinessName';
$matchCompany->in0->entityType = '';
$matchCompany->in0->listOfSimilars = true;

try
{  
    $result = $soapClient->matchCompany($matchCompany);     
    print_r($result); 
}
catch(SoapFault $fault)
{  
   echo $fault->faultcode . "-" . $fault->faultstring;  
   echo "REQUEST:\n" . htmlentities($soapClient->__getLastRequest()) . "\n"; 
}