Nusoap passed object array

Source: Internet
Author: User
Tags php error
: This article mainly introduces the nusoap passing object array. if you are interested in the PHP Tutorial, refer to it. Original article: http://www.cnblogs.com/Jaypei/archive/2009/04/09/1432521.html

The previous successful use of nuSOAP to transmit objects often requires remote return of object arrays during actual production. It took me one afternoon to successfully test, so I can't wait to share it with you :)

Preparations

First define a class UserInfo:

Class UserInfo {
Var $ UserName;
//... Var $ Sequence;
}

Then write a remote method for testing:

Function hello (){
$ A = new UserInfo ();
$ A-> UserName = "Jaypei ";
$ A-> Sequence = 1928388199;
$ B = new UserInfo ();
$ B-> UserName = "cnblogs ";
$ B-> Sequence = 83910021;
Return array ($ a, $ B );
}


A normal nuSOAP program is as follows: (the whole process is to transform it)

$ Soap = new soap_server ();
// Use UTF-8 $ soap-> soap_defencoding = 'utf-8 ';
$ Soap-> decode_utf8 = false;
// Set the WSDL namespace. suppose jaypei.cnblogs.com $ soap-> configureWSDL ('jaypei .cnblogs.com ', 'urn: jaypei.cnblogs.com ');
// ...... $ Soap-> register ('Hello ');

$ Soap-> service ($ HTTP_RAW_POST_DATA );

Transformation process

First, register a UserInfo compound type. the method is as follows: (this has been written before)

$ Soap-> wsdl-> addComplexType (
'Userinfo ',
'Complextype ',
'Struct ',
'All ',
'',
Array (
'Username' => array ('name' => 'username', 'type' => 'xsd: String '),
'Sequence '=> array ('name' => 'sequence', 'type' => 'xsd: Int ')
));

In this way, a single UserInfo object can be returned in the following way: ($ a or $ B is returned in hello)

$ Soap-> register ('hello', array (), array ('Return '=> 'tns: userinfo '));

If you want to return a list, you need to change the type at return. At first, we found the method to return the list of basic types such as string or int, as follows:

$ Soap-> register ('hello', array (), array ('Return '=> 'soap-ENC: array '));

When this method uses a custom composite type, the type changes:

Xsi: type = "SOAP-ENC: Array" SOAP-ENC: arrayType = "object [2]"

Object is not what we want. As a result, the following methods were found when I searched for relevant information online:

$ Soap-> register ('hello', array (), array ('Return '=> 'tns: userinfoarray '));

Here you need to add an array type of tns: UserInfoArray by using the following method:

$ Soap-> wsdl-> addComplexType (
'Userinfoarray ',
'Complextype ',
'Array ',
'',
'Soap-ENC: array ',
Array (),
Array (
Array ('ref '=> 'soap-ENC: arraytype', 'wsdl: arraytype' => 'tns: UserInfo []')
),
'Tns: userinfo'
);

At this time, the code is complete, but an error is reported when nuSOAP is called. it is a PHP error:

Catchable fatal error: Object of class userinfococould not be converted to string in... \ nusoap. php on line 6002

So we can find line 6002 of nusoap. php to see why UserInfo is converted to string:

$ This-> debug ("serializing array element: $ k, $ v of type: $ typeDef [arrayType]");

I did not read much of the code, but it is obviously only a debug code, which has no practical effect. So directly comment out and continue calling. OK! Now, everything goes smoothly.

Note: I still don't know this error because my use method violates the design intent or the bug of nuSOAP itself. if I have repeatedly listed some applications that have no problems, I will succeed, if you have any errors, please correct them.

The above introduces the nusoap object array, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.