The use of PHP's soap Nusoap

Source: Internet
Author: User
Tags soap wsdl

Today I don't know why I think of soap, and then I get it, and I use Nusoap on PHP. Some basic use, advanced trouble yourself to read the manual to go
The download of this software under Http://sourceforge.net/projects/nusoap/files/nusoap/0.9.5/nusoap-0.9.5.zip/download I will add the attachment.

Talk about this thing, I thought I could use this thing to complete the cross-domain landing, but today I found that it seems to not achieve,
In the end where can be applied, but also very vacant, I hope to know where the application of the friends pointing, thank you!

On the specific principles of soap, what I do not say, want to know what you find on the Internet, basically is the HTTP protocol and XML application.
First of all, the realization of the next thing, basically send a request to the server to implement a function on a server run, and then get the return value.
Value, I feel that it should be a string or an array, or an int value that should not be used by the object.

First look at a basic use very simple
This is the soap_server.php file that can be found on the Web.

include_once("lib/nusoap.php");//Insert File$server=NewSoap_server ();//Build Object$server->CONFIGUREWSDL ("test_wsdl", "" ");$server->wsdl->schematargetnamespace= "URN:TEST_WSDL";$server->register ("Hello",//Method NameArray("Name" = "xsd:string", "call" = "xsd:string", "Tele" and "xsd:string",),//Input ParametersArray("Return" = "xsd:string",),//Output Parameters"URN:TEST_WSDL",//name Space"Urn:test_wsdl#hello",//Name Space # function name to manipulate"RPC",//style"Encoded",// Use"This is test."//Description);//test Method ImplementationfunctionHello$name,$call,$tele) {if($name==""){return NewSoap_fault ("Client", "", "must supply a valid name.");}return"Hello,".$name." ".$call." ".$tele;}//Use the request to invoke the service$HTTP _raw_post_data=isset($HTTP _raw_post_data)?$HTTP _raw_post_data:"";$server->service ($HTTP _raw_post_data);

After you have finished, enter the URL in the browser, you should see a page, according to that page analysis, there will be a great harvest,
The client side should be based on the above usage
This is soap _client.php.

include_once("lib/nusoap.php");//Insert File//Set parameter array$para=Array("Name" = "sera1ph". Liu "," call "=" 123456 "," phone "=" 12345678 ",);//build the Client object, note the following URL? WSDL is a file that must be added in front of the server$client=NewSoapClient ("Http://localhost/nusoap/soap_server.php?WSDL");//call/Use that function, notice the distinction between namespaces$return=$client->call (' Hello ',$para, "http://localhost/soap/test_wsdl", "Test_wsdl#hello");Print_r($return);

The above is a very basic use, I think not too much to say

Let's look at a more complex use

require_once(‘.. /.. /lib/nusoap.php ');//gets the value returned by returning an arrayfunctionValidate$userName,$randomNum){        $temp[' userName '] =$userName; $temp[' randomnum '] =$randomNum; return $temp;}//I want to do a set a Get method, but this time to find a problem, and then againfunctionRefresh$refresh _username,$refresh _randomnum){        $temp[' userName '] =$userName; $temp[' randomnum '] =$randomNum; return $temp;}functionLogout$logout _username)//Log off user name (log out){$flag=$logout _username; if($logout _username=="" ||$logout _username! = "three Stones")        {        $flag=-1; }return $flag;}$soap=Newsoap_server ();//parameter one, service name, i.e. server file//parameter two, namespace$soap->CONFIGUREWSDL (' authorityservicewsdl ', ' urn:authorityservicewsdl ');//parameter setting space$soap->wsdl->schematargetnamespace = ' urn:authorityservicewsdl ';//The above is the same as the original, but people notice that the time of the declaration has changed//the following is a set of the returned array type//parameter one, for yourself named data type//Parameter two, mixed type, without tube//parameter three. For the struct, or array (array)//parameter four, according to what sort, there are three options all (All) |sequence (order) |choice (SELECT)//Parameter five, basic constraints//NOTE: Above 5 We do not have to change//parameter 6, the most important parameter, That is, we return the type to correspond//return array subscript =>array (' name ' = ' = ' array subscript ', ' type ' = ' = ' xsd: corresponding type ')//Type basic includes String,int, Date,boolean these several forms $soap->wsdl->addcomplextype (' UserInfo ', ' complexType ', ' struct ', ' all ', ',Array(            ' UserName ' =Array(' name ' = ' userName ', ' type ' = ' xsd:string '), ' randomnum ' =Array(' name ' = ' randomnum ', ' type ' = ' xsd:string ')                        )                );//here is the registration function//parameter one, the function name//parameter two, is the parameter of the functions accepted, note the specified type//parameter three, for the return value, not necessarily the name is called return, called the other can also, note the type of return,//I am here to return my own definition of the type TNS: UserInfo if the basic type is xsd:string this way//other parameters, as long as the unification is possible, especially the namespace with SOAPAction$soap->register (' Validate ',//Method Name    Array(' userName ' = ' xsd:string ', ' randomnum ' = ' xsd:string '),//Input Parameters//array (' return ' = ' xsd:string '),//Output parameters    Array(' return ' = ' tns:userinfo '), ' urn:authorityservicewsdl ',//namespace' Urn:authorityservicewsdl#validate ',//SOAPAction' RPC ',//style' Encoded ',// Use' Verify user name and random number '//Documentation);$soap->register (' Refresh ',//Method Name    Array(' refresh_username ' = ' xsd:string ', ' refresh_randomnum ' = ' xsd:string '),//Input Parameters//array (' return ' = ' xsd:string '),//Output parameters        Array(' return ' = ' tns:userinfo '), ' urn:authorityservicewsdl ',//namespace' Urn:authorityservicewsdl#refresh ',//SOAPAction' RPC ',//style' Encoded ',// Use' Place on time '//Documentation);$soap->register (' logout ',//Method Name    Array(' logout_username ' = ' xsd:string '),//Input Parameters    Array(' return ' = ' xsd:int '),//Output Parameters' Urn:authorityservicewsdl ',//namespace' Urn:authorityservicewsdl#logout ',//SOAPAction' RPC ',//style' Encoded ',// Use' Log out '//Documentation);//You may be confused about $HTTP _raw_post_data, we have no definition, how to come out//I also have this idea, but I read the manual to explain that usually is the value of the $HTTP _raw_post_data So let's just leave it and use it.$soap->service ($HTTP _raw_post_data);

We now look at the use of the client

require_once(‘.. /.. /lib/nusoap.php ');$client=NewSoapClient (' http://localhost/nusoap/samples/server/AuthorityService.php?wsdl ',true);$err=$client-GetError ();$username= "Three Stones";$randomnum= "D8A9";$params 1=Array(' userName ' =$username, ' randomnum ' =$randomnum);$reversed=$client->call (' Validate ',$params 1);//This is the return value//echo "registered value <br>";Print_r($reversed);Echo"<br>";$refresh _username= "Three Stones";$refresh _randomnum= "D8A9";$params 1=Array(' refresh_username ' =$refresh _username, ' refresh_randomnum ' =$refresh _randomnum);$reversed=$client->call (' Refresh ',$params 1);//echo "Get value <br>";Print_r($reversed);Echo"<br>";$logout _username= "Three Stones";$params 1=Array(' logout_username ' =$logout _username);$reversed=$client->call (' logout ',$params 1);Echo $reversed;

This does not have to say, a look should understand that is the array returned I use the Print_r output
about this feature, I say I do the experiment
first I set the value of the next public variable with the set method, when obtained with GET, the value is empty
description, Each time a new access is established so the value does not persist
Test 2, I use the value set by the cookie, when the Get method is not obtained
Description: No reason to understand why
Test 3, use session    In the Get method, also did not get the value
Description, do not understand
but get the file or database connection to obtain the value is not a problem, that is, can get the value of fixed media, file or database

What the hell is this thing really unclear, I think it can be done, 2 different languages of the handover, such as PHP and ASP and JSP interaction
Maybe someone is dazed, I lost a gratitude here, SOAP returns a stream, all can be treated as a text stream,
Use the socket to get. The use of the
about Nusoap is here, I do not go to the specific principle of it, can be used is enough! So there is no parsing of the Nusoap code

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.