Home  >  Article  >  Backend Development  >  nusoap passing array of objects

nusoap passing array of objects

WBOY
WBOYOriginal
2016-07-30 13:30:111174browse

Original text: http://www.cnblogs.com/Jaypei/archive/2009/04/09/1432521.html

The last time I successfully used nuSOAP to transfer objects, there is often another requirement in the actual production process, which is Remotely returns an array of objects. It took me an afternoon to finally test it successfully, so I can’t wait to share it with everyone:)

Preparation

First define a class UserInfo:

class UserInfo {
   
var $UserName;
//... var $Sequence;
}

Then write a test Remote method:

nusoap passing array of objects

function hello() {
$a = new UserInfo();
$a ->UserName = "Jaypei";
 
$a->Sequence = 1928388199 ;
$b = newUserInfo();
$b->UserName = "cnblogs";
​​
$b->Sequence = 83910021;
return array($a, $b);
}

nusoap passing array of objects


A common nuSOAP program is as follows: (The whole process is a transformation of it)

nusoap passing array of objects

$soap =newsoap_server();
// Use UTF-8$soap->soap_defencoding = 'UTF-8';
$soap ->decode_utf8 =false;
// Need to set the WSDL namespace, assuming jaypei.cnblogs.com$soap->configureWSDL(' jaypei.cnblogs ... > ;register('hello');$soap
->service($HTTP_RAW_POST_DATA); Transformation process
First, register a composite type of UserInfo as follows: (This has been written before)

$soap->wsdl->addComplexType(
)
'complexType',
                                                                                                      ''
, array( )
'UserName'=>array
(
'name'=> ,
' type
'
=>'
xsd:string'), '=>array( 'name'=>'Sequence', 'type' =>'xsd:int '
)
); ('hello', array(),array('return'=>'tns:UserInfo'));If you want to return a list, you need to change the type at return. Initially I found a list method that returns basic types such as string or int, as follows: $soap->register('hello', array(), array('return'=>'SOAP-ENC:Array') ); When this method is used on a custom composite type, the type will become: xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="object[2]"object is not What we want. So I searched for relevant information online and found the following method: $soap->register('hello', array(), array('return'=>'tns:UserInfoArray') );Here you need to customize Define and add an array type of tns:UserInfoArray, the method is as follows: $soap->wsdl->addComplexType(                                                       . 'UserInfoArray' ,

nusoap passing array of objects'

complexType

'

,


''

,

'

SOAP-ENC:Array

'

,

​​​​​​

arraynusoap passing array of objects()

,​​​​ array(
'
ref' => tns:UserInfo[]' )
)
, 'tns:UserInfo
'
); The code part has been completed at this time, but nuSOAP reported an error when calling. It's a PHP error: Catchable fatal error: Object of class UserInfocould not be converted to string in ...nusoapnusoap.php on line 6002So find line 6002 of nusoap.php and see why UserInfo is converted to string:
$this->debug("serializing array element: $k, $v of type: $typeDef[arrayType]");
I didn’t read much of the code, but it’s obviously just a debug code and has no practical effect. So just comment it out and continue calling, OK! Passed, everything went well. Note: I don’t know if this error is because my usage method violates the design intention or is a bug of nuSOAP itself. I have repeatedly listed some applications and there are no problems. It is considered a success. If there are any mistakes, please correct me.

The above introduces the object array passed by nusoap, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn