Php soap array one element

SOAP

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.

/*
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 );
>
>
>

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 ;

$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()

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.

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 typical error message if you provide a wsdl 2.0 format file :
PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn’t find in ‘wsdl/example.wsdl’ in /path/client.php on line 9

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.

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.

Here is an example of a php client talking to a asmx server:

// Prepare SoapHeader parameters
$sh_param = array(
‘Username’ => ‘username’ ,
‘Password’ => ‘password’ );
$headers = new SoapHeader ( ‘http://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 ;

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

Источник

Предопределённые константы

Перечисленные ниже константы определены данным модулем и могут быть доступны только в том случае, если PHP был собран с поддержкой этого модуля или же в том случае, если данный модуль был динамически загружен во время выполнения.

Константы SOAP

Константа Значение Описание
SOAP_1_1 ( int ) 1 Определяет использование SOAP 1.1 при передаче в качестве параметра soap_version методу SoapServer::__construct() или SoapClient::__construct() .
SOAP_1_2 ( int ) 2 Определяет использование SOAP 1.2 при передаче в качестве параметра soap_version методу SoapServer::__construct() или SoapClient::__construct() .
SOAP_PERSISTENCE_SESSION ( int ) 1
SOAP_PERSISTENCE_REQUEST ( int ) 2
SOAP_FUNCTIONS_ALL ( int ) 999
SOAP_ENCODED ( int ) 1 Определяет использование кодировки SOAP при передаче в качестве параметра use методу SoapClient::__construct() .
SOAP_LITERAL ( int ) 2 Определяет использование специфической для сервиса кодировки при передаче в качестве параметра use методу SoapClient::__construct() .
SOAP_RPC ( int ) 1 Определяет использование привязки в стиле RPC при передаче в качестве параметра style методу SoapClient::__construct() .
SOAP_DOCUMENT ( int ) 2 Определяет использование привязки к документу при передаче в качестве параметра style методу SoapClient::__construct() .
SOAP_ACTOR_NEXT ( int ) 1
SOAP_ACTOR_NONE ( int ) 2
SOAP_ACTOR_UNLIMATERECEIVER ( int ) 3
SOAP_COMPRESSION_ACCEPT ( int ) 32 Определяет использование заголовка «Accept-Encoding» при передаче параметра compression методу SoapClient::__construct() .
SOAP_COMPRESSION_GZIP ( int ) 0 Определяет использование сжатия gzip при передаче параметра compression методу SoapClient::__construct() .
SOAP_COMPRESSION_DEFLATE ( int ) 16 Определяет использование сжатия deflate при передаче параметра compression методу SoapClient::__construct() .
SOAP_AUTHENTICATION_BASIC ( int ) 0 Определяет использование базовой аутентификации HTTP при передаче параметра authentication методу SoapClient::__construct() .
SOAP_AUTHENTICATION_DIGEST ( int ) 1 Определяет использование аутентификации HTTP Digest Authentication при передаче параметра authentication методу SoapClient::__construct() .
SOAP_SSL_METHOD_TLS ( int ) 0 Используется с устаревшим параметром ssl_method метода SoapClient::__construct() .
SOAP_SSL_METHOD_SSLv2 ( int ) 1 Используется с устаревшим параметром ssl_method метода SoapClient::__construct() .
SOAP_SSL_METHOD_SSLv3 ( int ) 2 Используется с устаревшим параметром ssl_method метода SoapClient::__construct() .
SOAP_SSL_METHOD_SSLv23 ( int ) 3 Используется с устаревшим параметром ssl_method метода SoapClient::__construct() .
UNKNOWN_TYPE ( int ) 999998
XSD_STRING ( int ) 101
XSD_BOOLEAN ( int ) 102
XSD_DECIMAL ( int ) 103
XSD_FLOAT ( int ) 104
XSD_DOUBLE ( int ) 105
XSD_DURATION ( int ) 106
XSD_DATETIME ( int ) 107
XSD_TIME ( int ) 108
XSD_DATE ( int ) 109
XSD_GYEARMONTH ( int ) 110
XSD_GYEAR ( int ) 111
XSD_GMONTHDAY ( int ) 112
XSD_GDAY ( int ) 113
XSD_GMONTH ( int ) 114
XSD_HEXBINARY ( int ) 115
XSD_BASE64BINARY ( int ) 116
XSD_ANYURI ( int ) 117
XSD_QNAME ( int ) 118
XSD_NOTATION ( int ) 119
XSD_NORMALIZEDSTRING ( int ) 120
XSD_TOKEN ( int ) 121
XSD_LANGUAGE ( int ) 122
XSD_NMTOKEN ( int ) 123
XSD_NAME ( int ) 124
XSD_NCNAME ( int ) 125
XSD_ID ( int ) 126
XSD_IDREF ( int ) 127
XSD_IDREFS ( int ) 128
XSD_ENTITY ( int ) 129
XSD_ENTITIES ( int ) 130
XSD_INTEGER ( int ) 131
XSD_NONPOSITIVEINTEGER ( int ) 132
XSD_NEGATIVEINTEGER ( int ) 133
XSD_LONG ( int ) 134
XSD_INT ( int ) 135
XSD_SHORT ( int ) 136
XSD_BYTE ( int ) 137
XSD_NONNEGATIVEINTEGER ( int ) 138
XSD_UNSIGNEDLONG ( int ) 139
XSD_UNSIGNEDINT ( int ) 140
XSD_UNSIGNEDSHORT ( int ) 141
XSD_UNSIGNEDBYTE ( int ) 142
XSD_POSITIVEINTEGER ( int ) 143
XSD_NMTOKENS ( int ) 144
XSD_ANYTYPE ( int ) 145
XSD_ANYXML ( int ) 147
APACHE_MAP ( int ) 200
SOAP_ENC_OBJECT ( int ) 301
SOAP_ENC_ARRAY ( int ) 300
XSD_1999_TIMEINSTANT ( int ) 401
XSD_NAMESPACE ( int ) http://www.w3.org/2001/XMLSchema
XSD_1999_NAMESPACE ( int ) http://www.w3.org/1999/XMLSchema
SOAP_SINGLE_ELEMENT_ARRAYS ( int ) 1 Используется с параметром features метода SoapClient::__construct() .
SOAP_WAIT_ONE_WAY_CALLS ( int ) 2 Используется с параметром features метода SoapClient::__construct() .
SOAP_USE_XSI_ARRAY_TYPE ( int ) 4 Используется с параметром features метода SoapClient::__construct() .
WSDL_CACHE_NONE ( int ) 0 Отключает кеш WSDL при использовании с параметром soap.wsdl_cache или параметром wsdl_cache методов SoapClient::__construct() и SoapServer::__construct() .
WSDL_CACHE_DISK ( int ) 1 Определяет использование кеша WSDL на диске только при использовании с параметром soap.wsdl_cache или параметром wsdl_cache методов SoapClient::__construct() и SoapServer::__construct() .
WSDL_CACHE_MEMORY ( int ) 2 Определяет использование кеша WSDL в памяти только при использовании с параметром soap.wsdl_cache или параметром wsdl_cache методов SoapClient::__construct() и SoapServer::__construct() .
WSDL_CACHE_BOTH ( int ) 3 Определяет использование кеша WSDL как на диске, так и в памяти только при использовании с параметром soap.wsdl_cache или параметром wsdl_cache методов SoapClient::__construct() и SoapServer::__construct() .

User Contributed Notes 1 note

Источник

Читайте также:  Today's Date
Оцените статью