Phpsoap call webservice Application Test-PHP source code

Source: Internet
Author: User
Tags types of functions xml parser
SoapClient, which comes with php5. Of course, you can also use nusoap. php, a class written in pure php. The number of lines of code in this class is more than 7 K, which is definitely less efficient than SoapClient. SoapClient, which comes with php5. Of course, you can also use nusoap. php, a class written in pure php. The number of lines of code in this class is more than 7 K, which is definitely less efficient than SoapClient.

Script ec (2); script

1. Search for WebService sources

WebService can be compiled by yourself, but it can also be found from the network. I use US Zip Validator in www.xmethods.net. Its WSDL file is located at: http://www.webservicemart.com/uszip.asmx? WSDL. It is used to return the American place name, state name, longitude and latitude corresponding to the input ZIP code.

Ii. Create SoapClient

The second step is to create the SoapClient, call the WebService method, and obtain the return value. The PHP code is as follows:


Ii. Create SoapClient

The second step is to create the SoapClient, call the WebService method, and obtain the return value. The PHP code is as follows:

The Code is as follows:

$ ObjSoapClient = new SoapClient ("http://www.webservicemart.com/uszip.asmx? WSDL ");

$ Param = array ("ZipCode" => $ zip );
$ Out = $ objSoapClient-> ValidateZip ($ param );
$ Data = $ out-> ValidateZipResult;

There are many methods to create SoapClient. We use the most standard (and simplest) WSDL method. Because the ZIP query method requires a parameter, we must create an array and assign values using "parameter name => value.

Readers may be interested in the creation of this array. For example, how do we know that "parameter name" should be "ZipCode" rather than anything else? Why is there no more parameters, but only one? OK. We will explain the problem later. This involves the interpretation of WSDL.

After the parameters are created, similarly, we call the SoapClient method ValidateZip and pass the parameters in. For the returned results, we use the $ data variable to retrieve what we are really interested in. Similarly, there is also a question about how to determine the method name. We will introduce it later.

If you also use PhpEd for PHP development and debugging, you can clear the relationship between $ data and $ out in the following debugging window:


Iii. parsing data

The data in $ data obtained above is the data in the standard XML structure. Therefore, in PHP, we need to create an XML parser to analyze the data. The Code is as follows:

The Code is as follows:

$ ParsedData = array ();

Function startElement ($ parser, $ name, $ attribs)
{
Global $ ParsedData;
Echo "<$ name ";
If (count ($ attribs )){
Foreach ($ attribs as $ k => $ v)
{
$ ParsedData [$ k] = $ v;

Echo "$ k =" $ v "";
}
}
Echo "> ";
}

Function endElement ($ parser, $ name)
{
Echo" ";
}

$ Xml_parser = xml_parser_create ();
Xml_parser_set_option ($ xml_parser, XML_OPTION_CASE_FOLDING, 1 );
Xml_set_element_handler ($ xml_parser, "startElement", "endElement ");

Echo"

";
if (!xml_parse($xml_parser, $data)) {
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
echo "
";

Xml_parser_free ($ xml_parser );

For detailed operations, see the section on XML functions in the PHP function manual. I will not go into details here. Once the data is successfully parsed, we can further process it. For example, the following code traverses the array and then outputs:

The Code is as follows:
Foreach ($ ParsedData as $ k => $ v)
{
Echo $ k. "=>". $ v ."
";
}

4. interpreting WSDL

We left two questions above: How do I know the method provided by a WebService and its parameters? All the answers are in the WSDL description. For the WSDL used in this article, we will take a section from it for analysis. Since we call it through Soap, I have excerpted the complete WSDL, listing only the Soap call Section (reverse-display part ):

First, we noticed that In this section, it is pointed out that in the Soap call, the entry parameter should refer to ValidateZip, so we will go to the point above the file to see the definition of the ValidateZip method:

The Code is as follows:







Obviously, ValidateZip requires a parameter named ZipCode and of the string type.

Let's look at it again. In this section, it indicates that the exit parameter of the Soap call is ValidateZipResponse. The latter outgoing parameter name is ValidateZipResult. So we explained the question raised in the first two sections:

• How to Find the function to be called?
• How do I know the parameters and types of functions?
• How can I obtain the return value of a function?

Example 2,

Here we use the class that comes with php5 for operations

My structure is as follows: the webservice folder contains the following three files: Personinfo. php, SoapClient. php, and SoapServer. php. For more information, see comments in the code.

The Code is as follows:


/**
* Personinfo. php
* This class contains the methods to be called.
* @ Author itbdw
*
*/
Class Personinfo {

/**
* Return name
* @ Return unknown_type
*/
Public function getName (){
Return 'it tumbler ';
}

/**
* Returns a date in a specific format.
* @ Return unknown_type
*/
Public function getTime (){
Return date ('Y-m-d ');
}

}

The Code is as follows:

/**
* SoapServer. php
* Webservice server instance
*/
// Contains the class that provides the service
Require_once 'personinfo. php ';

// Modify the downstream content according to the actual situation
$ S = new SoapServer (null, array ("location" => "http: // zby/webservice/SoapServer. php "," uri "=>" SoapServer. php "));

$ S-> setClass ("PersonInfo ");

$ S-> handle ();
[/Php]
[Php]
/**
* SoapClient. php
* Webservice client instance
*/

Header ('content-Type: text/html; charset = UTF-8 ′);

Try {

// Modify the downstream content according to the actual situation
$ Soap = new SoapClient (null, array ('location' => 'HTTP: // zby/webservice/SoapServer. php', 'uri '=> 'soapserver. php '));

Echo $ soap-> getName ();
Echo $ soap-> getTime ();

} Catch (SoapFault $ e ){
Echo $ e-> getMessage ();
} Catch (Exception $ e ){
Echo $ e-> getMessage ();
}

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.