Php soap call webservice application test

Source: Internet
Author: User
Tags foreach getmessage soap php code types of functions xml parser zip

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: Copy code

$ 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 from the screenshot 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: Copy code

$ ParsedData = array ();

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

Echo "<font color =" #009900 "> $ k </font> =" <font color = "#990000"> $ v </font> "";
        }
    }
Echo "> ";
}

Function endElement ($ parser, $ name)
{
Echo "</<font color =" # 0000cc "> $ name </font> ";
}

$ 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 "<pre> ";
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 "</pre> ";

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: Copy code
Foreach ($ ParsedData as $ k => $ v)
{
Echo $ k. "=>". $ v. "<br/> ";
}

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 the <wsdl: message name = "ValidateZipSoapIn"> section, which indicates that in a Soap call, the entry parameters 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: Copy code

<S: element name = "ValidateZip">
<S: complexType>
<S: sequence>
<S: element minOccurs = "0" maxOccurs = "1" name = "ZipCode" type = "s: string"/>
</S: sequence>
</S: complexType>
</S: element>

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

Similarly, let's look at the <wsdl: message name = "ValidateZipSoapOut"> section, which points out 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: Copy code


<? Php
/**
* 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: Copy code

<? Php
/**
* 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]
<? Php
<? Php
/**
* SoapClient. php
* Webservice client instance
*/

Header ('content-Type: text/html; charset = utf-8 & prime ;);

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 ();
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.