Use of nusoap in soap (jieshijie.com)

Source: Internet
Author: User
Today, I don't know why I think of soap. Then I figured it out. In PHP, I used nusoap. For some basic usage, please read the manual.
The download of this software under the http://dietrich.ganx4.com/nusoap I will add the attachment.

I thought I could use this to complete cross-domain name login, but today I found that it cannot be implemented,
I am confused about where the app can be applied. I hope that my friends will give me some advice. Thank you!

I will not talk much about the specific principles of soap. If you want to know the specific principles on the Internet, you can find out the application of HTTP protocol and XML.
Let's talk about the implementation principle of the next thing. Basically, we send a request to the server to implement the function running on the server and then get the returned value.
Value. I think it should be a string, an array, or an int value. The object should not be used.

Let's take a look at a basic usage that is easy to use.
This is the soap_server.php file, which can be found on the network.

Code: [copy to clipboard] <? PHP
Include_once ("lib/nusoap. php"); // insert a file
$ Server = new soap_server (); // generate an object
$ Server-> configurewsdl ("test_wsdl ","");
$ Server-> WSDL-> schematargetnamespace = "urn: test_wsdl ";
$ Server-> Register ("hello", // method name
Array (
"Name" => "XSD: string ",
"Call" => "XSD: string ",
"Tele" => "XSD: string ",
), // Enter the Parameter
Array (
"Return" => "XSD: string ",
), // Output parameters
"Urn: test_wsdl", // namespace
"Urn: test_wsdl # Hello", // namespace # Name of the function to be operated
"RPC", // Style
"Encoded", // use
"This is test." // description
);
// Implement the test method
Function Hello ($ name, $ call, $ tele ){
If ($ name = ""){
Return new soap_fault ("client", "", "must supply a valid name .");
}
Return "hello,". $ name. "". $ call. "". $ tele;
}
// Use the request to (try 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 finish writing, enter the URL in the browser and you will see a page. According to the analysis on that page, there will be great gains,
The client must follow the above steps
This is soap _ client. php.

Code: [copy to clipboard] <? PHP
Include_once ("lib/nusoap. php"); // insert a file
// Set the parameter Array
$ Para = array (
"Name" => "sera1ph. Liu ",
"Call" => "123456 ",
"Phone" => "12345678 ",
);
// Generate the client object. What is the URL below? WSDL is a file that must be added to the server.
$ Client = new soapclient ("http: // localhost/nusoap/soap_server.php? WSDL ");
// Call/use that function. Note the differences 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 just want to talk about it more.

The following describes a complex application.

Code: [copy to clipboard] <? PHP
Require_once ('../lib/nusoap. php ');
// An array is returned when a value is obtained.
Function validate ($ username, $ randomnum)
{
$ Temp ['username'] = $ username;
$ Temp ['randomainnum'] = $ randomnum;
Return $ temp;
}
// I wanted to set a get method, but I found a problem at this time. I will discuss it later.
Function refresh ($ refresh_username, $ refresh_randomnum)
{
$ Temp ['username'] = $ username;
$ Temp ['randomainnum'] = $ randomnum;
Return $ temp;
}

Function logout ($ logout_username) // log out of the username (log out)
{
$ Flag = $ logout_username;

If ($ logout_username = "" | $ logout_username! = "Sanshi ")
{
$ Flag =-1;
}

Return $ flag;
}

$ Soap = new soap_server ();
// Parameter 1, service name, that is, Server File
// Parameter 2, namespace
$ Soap-> configurewsdl ('authorityservicewsdl ', 'urn: authorityservicewsdl ');
// Parameter setting Space
$ Soap-> WSDL-> schematargetnamespace = 'urn: authorityservicewsdl ';
// The above is basically the same as the original one, but the Declaration changes.
// Set the returned array type below
// Parameter 1: the data type you name yourself.
// Parameter 2, mixed type, no need to worry
// Parameter 3. Is a struct or Array)
// Parameter 4. sort by parameters. Three options are available: All | sequence | choice)
// Parameter 5: basic constraints
// Note: we do not need to change the above five items.
// Parameter 6, the most important parameter, that is, the type that we return
// Return array subscript => array ('name' => "array subscript", 'type' => "XSD: corresponding type ")
// The types include string, Int, date, and Boolean.
$ Soap-> WSDL-> addcomplextype ('userinfo', 'complextype', 'struct ', 'all ','',
Array (
'Username' => array ('name' => 'username', 'type' => 'xsd: string '),
'Randnum' => array ('name' => 'randnum', 'type' => 'xsd: string ')
)
);

// Here is the registration function
// Parameter 1, function name
// Parameter 2, which is a parameter accepted by this function. specify the type
// Parameter 3, which is the returned value. It is not necessarily called return. It can be called other values. Pay attention to the returned type,
// Here I return the custom type TNS: userinfo. If the basic type is XSD: string
// You only need to unify other parameters, especially namespace and 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
'Authentication username 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
'Time location '// 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
'Logout '// documentation
);
// You may be confused about the variable $ http_raw_post_data. We have no definition. How can this problem be solved?
// I have the same idea, but I have read the manual to explain that usually is the value of $ http_raw_post_data.
// Leave it alone. Use it like this.
$ Soap-> service ($ http_raw_post_data );
?>

Now let's take a look at the use of the client.

Code: [copy to clipboard] <? PHP
Require_once ('../lib/nusoap. php ');

$ Client = new soapclient ('HTTP: // localhost/nusoap/samples/Server/authorityservice. php? WSDL ', true );
$ Err = $ client-> geterror ();

$ Username = "sanshi ";
$ Randomnum = "d8a9 ";
$ Params1 = array ('username' => $ username, 'randomnum' => $ randomnum );
$ Reversed = $ client-> call ('validate', $ params1); // This is the returned value.
// Echo "registered value <br> ";
Print_r ($ reversed );
Echo "<br> ";

$ Refresh_username = "sanshi ";
$ Refresh_randomnum = "d8a9 ";
$ Params1 = array ('refresh _ username' => $ refresh_username, 'refresh _ randomnum' => $ refresh_randomnum );
$ Reversed = $ client-> call ('refresh', $ params1 );
// Echo "get value <br> ";
Print_r ($ reversed );
Echo "<br> ";

$ Logout_username = "sanshi ";
$ Params1 = array ('logout _ username' => $ logout_username );
$ Reversed = $ client-> call ('logout', $ params1 );
Echo $ reversed;
?>

I don't need to talk about this. At first glance, I should understand that the returned array is output using print_r.
Let me talk about this function.
First, I used the set method to set the value of the next public variable. When obtained using get, the value is null.
Note: each time a new access is created, the value is not retained.
Test 2: When I use the cookie setting value, the get method does not get
Note: The reason is not clear.
Test 3: no value is obtained when session is used in the get method.
Note: I am not confused.
However, there is no problem in obtaining the file or database connection value, that is, the value in the fixed media can be obtained.

What can this thing do? I think it can be done. Two different languages can be handed over, such as the interaction between PHP and ASP and JSP.
Some may be lost. I have lost a sentiment here. Soap returns a stream, which can be treated as a text stream,
Use socket.
The use of nusoap is here. I am not going to figure out the specific principles of it. It is enough to use it! Therefore, the nusoap code is not analyzed.

Author: sanshi
Mail: sanshi0815@tom.com
 

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.