PROBLEM (with SOAP extension under PHP5) of transferring object, that contains objects or array of objects. Nested object would not transfer.
SOLUTION:
This class was developed by trial and error by me. So this 23 lines of code for most developers writing under PHP5 solves fate of using SOAP extension.
<?php
/*
According to specific of organization process of SOAP class in PHP5, we must wrap up complex objects in SoapVar class. Otherwise objects would not be encoded properly and could not be loaded on remote SOAP handler.
Function "getAsSoap" call for encoding object for transmission. After encoding it can be properly transmitted.
*/
abstract class SOAPable {
public function getAsSOAP() {
foreach($this as $key=>&$value) {
$this->prepareSOAPrecursive($this->$key);
}
return $this;
}
private function prepareSOAPrecursive(&$element) {
if(is_array($element)) {
foreach($element as $key=>&$val) {
$this->prepareSOAPrecursive($val);
}
$element=new SoapVar($element,SOAP_ENC_ARRAY);
}elseif(is_object($element)) {
if($element instanceof SOAPable) {
$element->getAsSOAP();
}
$element=new SoapVar($element,SOAP_ENC_OBJECT);
}
}
}
// ------------------------------------------
// ABSTRACT EXAMPLE
// ------------------------------------------
class PersonList extends SOAPable {
protected $ArrayOfPerson; // variable MUST be protected or public!
}
class Person extends SOAPable {
//any data
}
$client=new SoapClient("test.wsdl", array( 'soap_version'=>SOAP_1_2, 'trace'=>1, 'classmap' => array('Person' => "Person", 'PersonList' => "PersonList") ));
$PersonList=new PersonList;
// some actions
$PersonList->getAsSOAP();
$client->someMethod($PersonList);
?>
So every class, which will transfer via SOAP, must be extends from class SOAPable.
As you can see, in code above, function prepareSOAPrecursive search another nested objects in parent object or in arrays, and if does it, tries call function getAsSOAP() for preparation of nested objects, after that simply wrap up via SoapVar class.
So in code before transmitting simply call $obj->getAsSOAP()| CARVIEW |
Select Language
HTTP/2 301
server: myracloud
date: Fri, 26 Dec 2025 13:27:29 GMT
content-type: text/html
content-length: 161
location: https://www.php.net/soap
HTTP/2 200
server: myracloud
date: Fri, 26 Dec 2025 13:27:30 GMT
content-type: text/html; charset=utf-8
content-language: en
permissions-policy: interest-cohort=()
x-frame-options: SAMEORIGIN
status: 200 OK
link: ; rel=shorturl
last-modified: Fri, 26 Dec 2025 12:08:46 GMT
vary: accept-encoding
content-encoding: gzip
expires: Fri, 26 Dec 2025 13:27:30 GMT
cache-control: max-age=0
PHP: SOAP - Manual
update page now
SOAP
- Introduction
- Installing/Configuring
- Predefined Constants
- SOAP Functions
- is_soap_fault — Checks if a SOAP call has failed
- use_soap_error_handler — Set whether to use the SOAP error handler
- SoapClient — The SoapClient class
- SoapClient::__call — Calls a SOAP function (deprecated)
- SoapClient::__construct — SoapClient constructor
- SoapClient::__doRequest — Performs a SOAP request
- SoapClient::__getCookies — Get list of cookies
- SoapClient::__getFunctions — Returns list of available SOAP functions
- SoapClient::__getLastRequest — Returns last SOAP request
- SoapClient::__getLastRequestHeaders — Returns the SOAP headers from the last request
- SoapClient::__getLastResponse — Returns last SOAP response
- SoapClient::__getLastResponseHeaders — Returns the SOAP headers from the last response
- SoapClient::__getTypes — Returns a list of SOAP types
- SoapClient::__setCookie — Defines a cookie for SOAP requests
- SoapClient::__setLocation — Sets the location of the Web service to use
- SoapClient::__setSoapHeaders — Sets SOAP headers for subsequent calls
- SoapClient::__soapCall — Calls a SOAP function
- SoapServer — The SoapServer class
- SoapServer::addFunction — Adds one or more functions to handle SOAP requests
- SoapServer::addSoapHeader — Add a SOAP header to the response
- SoapServer::__construct — SoapServer constructor
- SoapServer::fault — Issue SoapServer fault indicating an error
- SoapServer::getFunctions — Returns list of defined functions
- SoapServer::__getLastResponse — Returns last SOAP response
- SoapServer::handle — Handles a SOAP request
- SoapServer::setClass — Sets the class which handles SOAP requests
- SoapServer::setObject — Sets the object which will be used to handle SOAP requests
- SoapServer::setPersistence — Sets SoapServer persistence mode
- SoapFault — The SoapFault class
- SoapFault::__construct — SoapFault constructor
- SoapFault::__toString — Obtain a string representation of a SoapFault
- SoapHeader — The SoapHeader class
- SoapHeader::__construct — SoapHeader constructor
- SoapParam — The SoapParam class
- SoapParam::__construct — SoapParam constructor
- SoapVar — The SoapVar class
- SoapVar::__construct — SoapVar constructor
+add a note
User Contributed Notes 7 notes
nodkz at mail dot ru ¶
17 years ago
Raphal Gertz ¶
16 years ago
Juste a note to avoid wasting time on php-soap protocol and format support.
Until php 5.2.9 (at least) the soap extension is only capable of understanding wsdl 1.0 and 1.1 format.
The wsdl 2.0, a W3C recommendation since june 2007, ISN'T supported in php soap extension.
(the soap/php_sdl.c source code don't handle wsdl2.0 format)
The wsdl 2.0 is juste the 1.2 version renamed because it has substantial differences from WSDL 1.1.
The differences between the two format may not be invisible if you don't care a lot.
The wsdl 1.0 format structure (see https://www.w3.org/TR/wsdl) :
<definitions ...>
<types ...>
</types>
<message ...>
<part ...>
</message>
<portType ...>
<operation ...>
<input ... />
<output ... />
<fault ... />
</operation>
</portType>
<binding ...>
<operation ...>
<input ... />
<output ... />
<fault ... />
</operation>
</binding>
<service ...>
<port ...>
</service>
</definitions>
And the wsdl 2.0 format structure (see https://www.w3.org/TR/wsdl20/) :
<description ...>
<types ...>
</types>
<interface ...>
<fault ... />
<operation ...>
<input ... />
<output ... />
<fault ... />
</operation>
</interface>
<binding ...>
<fault ... />
<operation ...>
<input ... />
<output ... />
<fault ... />
</operation>
</binding>
<service ...>
<endpoint ...>
</service>
</description>
The typical error message if you provide a wsdl 2.0 format file :
PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't find <definitions> in 'wsdl/example.wsdl' in /path/client.php on line 9
moazzam at moazzam-khan dot com ¶
16 years ago
If anyone is trying to use this for accessing Sabre's web services, it won't work. Sabre checks the request header "Content-Type" to see if it is "text/xml" . If it is not text/xml then it sends an error back.
You will need to create a socket connection and use that to send the request over.
Ryan ¶
17 years ago
If you are having an issue where SOAP cannot find the functions that are actually there if you view the wsdl file, it's because PHP is caching the wsdl file (for a day at a time). To turn this off, have this line on every script that uses SOAP: ini_set("soap.wsdl_cache_enabled", "0"); to disable the caching feature.
stephenlansell at gmail dot com ¶
15 years ago
Here is an example of a php client talking to a asmx server:
<?php
$soapClient = new SoapClient("https://soapserver.example.com/blahblah.asmx?wsdl");
// Prepare SoapHeader parameters
$sh_param = array(
'Username' => 'username',
'Password' => 'password');
$headers = new SoapHeader('https://soapserver.example.com/webservices', 'UserCredentials', $sh_param);
// Prepare Soap Client
$soapClient->__setSoapHeaders(array($headers));
// Setup the RemoteFunction parameters
$ap_param = array(
'amount' => $irow['total_price']);
// Call RemoteFunction ()
$error = 0;
try {
$info = $soapClient->__call("RemoteFunction", array($ap_param));
} catch (SoapFault $fault) {
$error = 1;
print("
alert('Sorry, blah returned the following ERROR: ".$fault->faultcode."-".$fault->faultstring.". We will now take you back to our home page.');
window.location = 'main.php';
");
}
if ($error == 0) {
$auth_num = $info->RemoteFunctionResult;
if ($auth_num < 0) {
....
// Setup the OtherRemoteFunction() parameters
$at_param = array(
'amount' => $irow['total_price'],
'description' => $description);
// Call OtherRemoteFunction()
$trans = $soapClient->__call("OtherRemoteFunction", array($at_param));
$trans_result = $trans->OtherRemoteFunctionResult;
....
} else {
// Record the transaction error in the database
// Kill the link to Soap
unset($soapClient);
}
}
}
}
?>
Luke ¶
10 years ago
Was calling an asmx method like $success=$x->AuthenticateUser($userName,$password) and this was returning me an error.
However i changed it and added the userName and password in an array and its now KAWA...
rafinskipg at gmail dot com ¶
13 years ago
Support for MTOM addign this code to your project:
<?php
class MySoapClient extends SoapClient
{
public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
$response = parent::__doRequest($request, $location, $action, $version, $one_way);
// parse $response, extract the multipart messages and so on
//this part removes stuff
$start=strpos($response,'<?xml');
$end=strrpos($response,'>');
$response_string=substr($response,$start,$end-$start+1);
return($response_string);
}
}
?>
Then you can do this
<?php
new MySoapClient($wsdl_url);
?>
↑ and ↓ to navigate •
Enter to select •
Esc to close • / to open
Press Enter without
selection to search using Google