PHP uses SOAP extension to implement WebService

Source: Internet
Author: User
This article mainly introduces PHP's method of implementing WebService using SOAP extension, and analyzes in detail the principles of SOAP extension and related techniques for implementing WebService based on the instance form, for more information about how to implement WebService using SOAP extensions in PHP, see the following example. We will share this with you for your reference. The details are as follows:

Recently in a PHP project docking external interface involves WebService, search engine related articles are not a lot, find the most are cited a very powerful open source software NuSOAP (: ), that is, some classes. The environment described in this article is PHP 4.3. Currently, PHP 5.2 or PHP 5.3 is popular. Try it first. An error occurred while running. the soapclient class provided by NuSOAP conflicts with the SoapClient class added with the built-in SOAP extension in PHP 5.

Although NuSOAP can be used in all PHP environments, it is not affected by server security settings. However, if you need to reference a large number of class files, you still feel that the built-in SOAP extension is better in PHP 5, which is practical. First, let's take a look at SOAP:

1. Comparison of SOAP and XML-PRC

In the early stages of Web service development, the first major purpose of formatting messages in XML is to apply messages to XML-RPC protocols where RPC represents remote procedure calls. In XML remote procedure call (XML-RPC), the client sends a specific message that must contain a name, a program that runs the service, and input parameters.

XML-RPC can only use a limited number of data types and some simple data structures. People think that this protocol is not powerful enough, so SOAP emerged-its initial definition is Simple Object Access Protocol. Later, we gradually realized that SOAP is not simple, and we do not need to use object-oriented languages. Therefore, people only use the name SOAP.

XML-RPC has only a simple set of data types. Instead, SOAP defines data types by utilizing the continuous development of XML Schema. At the same time, SOAP can also take advantage of the XML namespace, which is not required by the XML-RPC. In this way, the beginning of a SOAP message can be an XML namespace declaration of any type, at the cost of increasing complexity and incompatibility between systems.

With the awakening of the computer industry, people have discovered the commercial potential of XML-based Web services. as a result, companies are constantly exploring ideas, ideas, arguments, and standardization attempts. W3C once managed to organize achievements in the name of "Web service activities", including XML Protocol Working Group that actually makes SOAP ). The number of standardized results related to Web services (in a sense SOAP-related or SOAP-dependent) has multiplied to a surprising extent.

Initially, SOAP evolved as an extension of the XML-RPC, primarily emphasizing remote process calls through methods and variable names obtained from the WSDL file. Now, through continuous improvement, people have found more ways to use SOAP, rather than simply using the "file" method-basically using a SOAP envelope to transfer XML formatted files. In any case, to understand SOAP, understanding the role played by WSDL is the most fundamental.

II. Structure Analysis of SOAP data packets

A SOAP message is called a SOAP Envelope, including the SOAP Header and SOAP Body. Among them, the SOAP Header can easily insert a variety of other messages to expand the Web Service functions, such as Security (using certificates to access Web Service), SOAP Body is the specific message Body, that is, the information after Marshall.

When calling SOAP, that is, sending an HTTP Post packet to a URL (such as a (according to the SOAP specification, HTTP Get packets can also be supported ), the name of the call method is provided in the HTTP Request Header SOAP-Action, followed by the SOAP Envelope. The server receives the request and executes the computation. it converts the returned result Marshall into XML and returns it to the client over HTTP.

III. simple SOAP example

There are three methods for SOAP development:

1). SOAP extensions provided by PEAR;
2) PHP built-in SOAP extension;
3). NuSOAP (pure PHP ).

The built-in SOAP extension is added to PHP 5 and is provided as part of PHP. Therefore, you do not need to download, install, and manage separate packages. This is the first SOAP implementation written in PHP instead of C, so the author claims that it is much faster. Relevant documents are included in the Function Reference section of the PHP Manual (php_soap.dll ).

An example of a client accessing the. net web service:

< ? php$objSoapClient = new SoapClient("");$param = array("ZipCode"=>'12209');$out = $objSoapClient->ValidateZip($param);$data = $out->ValidateZipResult;echo $data;?>

IV. instances

1) use PHP to create a SOAP service

Create soap_server.php (virtual path: http: // localhost/php/soap/soap_server.php)

< ? php/*** A simple math utility class*/class math{  /**  * Add two integers together  *  * @param integer $a The first integer of the addition  * @param integer $b The second integer of the addition  * @return integer The sum of the provided integers  */  public function add($a, $b){    return $a + $b;  }  /**  * Subtract two integers from each other  *  * @param integer $a The first integer of the subtraction  * @param integer $b The second integer of the subtraction  * @return integer The difference of the provided integers  */  public function sub($a, $b){    return $a - $b;  }  /**  * Div two integers from each other  *  * @param integer $a The first integer of the subtraction  * @param integer $b The second integer of the subtraction  * @return double The difference of the provided integers  */  public function p($a, $b){    if($b == 0){      throw new SoapFault(-1, "Cannot pide by zero!");    }    return $a / $b;  }}$server = new SoapServer('math.wsdl', array('soap_version'=>SOAP_1_2));$server->setClass("math");$server->handle();?>


A) the math class is the webservice to be published;
B), $ server-> setClass, not $ server-> addClass.
2) use the PHP client to access the SOAP service just created

< ? php// $client = new SoapClient('http://localhost/php/soap/math.wsdl');$client = new SoapClient("http://localhost/php/soap/soap_server.php?WSDL");try{  $result = $client->p(8, 2); // will cause a Soap Fault if pide by zero  print "The answer is: $result";}catch(SoapFault $e){  print "Sorry an error was caught executing your request: {$e->getMessage()}";}?>

In essence, http: // localhost/php/soap/soap_server.php? To access the WSDL description file referred to by the comment line, the wsdl file must be generated in advance. Other languages such as Java can be dynamically generated. For PHP built-in SOAP extensions, this WSDL file must be generated in advance.

You can use ZendStudio to generate a static WSDL file. in this case, phpdoc of the math class is used as the metadata for generating the WSDL. When using ZendStudio to generate a wsdl file, you must correctly describe the target address of the Web service. The snippets are as follows:


Note: The name parameter must be input to call the PHP Webserver method.

For more articles about how PHP uses SOAP extension to implement WebService, refer to PHP Chinese network!

Related Article

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