Nusoap passing an array of objects

Source: Internet
Author: User
Tags wsdl
Original: http://www.cnblogs.com/Jaypei/archive/2009/04/09/1432521.html

The last successful use of Nusoap passed objects, there is often another requirement in the actual production process, which is to return an array of objects remotely. Spent an afternoon of time finally successful test success, so impatient to come out to share:)

Preparatory work

First define a class UserInfo:

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

Then write a test using the remote method:

function Hello () {
$a = New UserInfo ();
$a - UserName = " Jaypei " ;
$a - Sequence = 1928388199 ;
$b = New UserInfo ();
$b - UserName = " Cnblogs " ;
$b - Sequence = 83910021 ;
return Array ( $a , $b );
}


An ordinary Nusoap program is as follows: (the whole process is to transform it)

$soap = New Soap_server ();
// using UTF-8 $soap - soap_defencoding = ' UTF-8 ' ;
$soap - Decode_utf8 = false ;
// the WSDL namespace needs to be set, assuming 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 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 ' )
));

This allows you to return a single UserInfo object in the following way: (return $ A in Hello or return $b)

$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, a list method that returns an underlying type such as string or int is found, 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 the Internet to find relevant information found the following methods:

$soap->register (' Hello ', Array (), array (' return ' = ' Tns:userinfoarray ') );

Here to customize the addition of an array type of Tns:userinfoarray, the method is as follows:

$soap - WSDL - Addcomplextype (
' Userinfoarray ' ,
' ComplexType ' ,
' Array ' ,
'' ,
' Soap-enc:array ' ,
Array () ,
Array (
Array ( ' ref ' = ' Soap-enc:arraytype ' , ' Wsdl:arraytype ' = ' tns:userinfo[] ' )
) ,
' Tns:userinfo '
);

At this point the Code section has completed, but the call Nusoap error, is a PHP bug:

Catchable fatal error:object of class userinfocould not being converted to string in ... \nusoap\nusoap.php on line 6002

So I found Nusoap.php's 6002 line to see why UserInfo is converting to string:

$this->debug ("Serializing array element: $k, $v of type: $typeDef [arraytype]");

The code doesn't look much, but it's obviously just a debug code that doesn't really work. Then directly comment out the continuation of the call, ok! Passed, and everything went well.

Note: This error is not known now because my use of the method has violated the design intent or nusoap itself bug, repeatedly enumerated some applications have no problem after the success, if there are any errors also please correct me.

The above describes the Nusoap transfer object array, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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