使用PHP调用SOAP Web服务的问题


Problems Calling SOAP Web Service Using PHP

这是我的SOAP请求:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:impl="http://impl.user">
<soapenv:Header/>
<soapenv:Body>
  <impl:UserSessionDetails soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
     <in0 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">requstXml</in0>
     <in1 xsi:type="impl:UserInfo">
        <password xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">pwd</password>
        <userName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">pwd</userName>
     </in1>
  </impl:UserSessionDetails>
 </soapenv:Body>
</soapenv:Envelope>

我尝试了以下PHP代码:

$soapClient = new SoapClient("http://example.com/services/?WSDL");
$sh_param = array(
    'userName'    =>    'user',
    'Password'    =>    'pwd'
);
$headers = new SoapHeader('http://tempuri.org/', false);
$soapClient->__setSoapHeaders(array($headers));
$requestXML = '<request name= "UserSessionDetails"><UserDetails><user_name>username</user_name><from_date>fromdate</from_date><to_date>to_date</to_date><group></group></UserDetails></request>';
$result = $soapClient->UserSessionDetails(array('in0'=> $requestXML, 'in1'=> $sh_param));
$simple = $result->$simple = $result->UserSessionDetailsResponse;
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "<pre>";
print_r($vals);
echo "</pre>"

但它一无所获。有人能告诉我问题出在哪里吗?

不要在SoapClient请求中使用原始xml。请改用对象/数组或SoapVar。

基本示例:

<?php
$client = new 'SoapClient('http://example.com/services/?WSDL');
// Optionally your service endpoint
$client->__setLocation('http://example.com/services'); 
$userDetails = new stdClass();
$userDetails->user_name = 'username';
$userDetails->from_date = 'fromdate';
$userDetails->to_date = 'to_date';
$in0 = new stdClass();
$in0->UserDetails = $userDetails;
$in1 = new stdClass();
$in1->userName = 'pwd';
$in1->Password = 'pwd';
$request = new stdClass();
$request->in0 = $in0;
$request->in1 = $in1;
$result = $client->UserSessionDetails($request);

您可以使用任何SOAP客户端(如SOAPUI)调试SOAP服务

p.S。看看Zend_Soap_Client和SoapVar示例