简体   繁体   中英

Problems Calling SOAP Web Service Using PHP

This is my SOAP request:

<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>

I've tried the following PHP code:

$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>"

But it returns nothing. Can anybody please tell me what the problem is?

Do not use raw xml in SoapClient requests. Use objects/arrays or SoapVar instead.

Basic example:

<?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);

You can debug you SOAP services with any SOAP client like SOAPUI

PS Have a look at Zend_Soap_Client and SoapVar examples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM