PHP uses SOAP extensions to implement WebService methods _php tips

Source: Internet
Author: User
Tags data structures http post soap web services wsdl

The examples in this article describe the way PHP implements WebService using SOAP extensions. Share to everyone for your reference, specific as follows:

Recently in a PHP project docking external interface involves webservice, search engine related articles are not many, find mostly refers to a so-called very powerful Open-source software nusoap (download address: http://sourceforge.net/projects/ nusoap/), i.e. some classes. The article is written to describe the environment is PHP 4.3, now popular PHP 5.2 or PHP 5.3. Try it first, run wrong, the original Nusoap provided soapclient class conflicts with the SoapClient class of the built-in SOAP extensions added to PHP 5.

Although Nusoap is known to be used in all PHP environments, it is not affected by server security settings. But need to refer to a lot of class files, or feel the use of PHP 5 to add a built-in SOAP extension better, to achieve practical. Let's take a look at soap:

I. SOAP and XML-PRC comparisons

In the early stages of web services development, the first major use of XML formatted messages was applied to the RPC protocol, where RPC represented a remote procedure call. In XML remote procedure call (RPC), the client sends a specific message that must include the name, the program that runs the service, and the input parameters.

XML-RPC can only use a limited range of data types and some simple data structures. People thought the protocol was not strong enough, so there was a soap--. Its original definition was a Simple Object access protocol. After that, it was gradually realized that soap was not simple, and that there was no need to use an object-oriented language, so now people just follow the name of soap.

XML-RPC has only a simple set of data types, and instead, SOAP defines the data type by taking advantage of the constant evolution of the xml-based schema. At the same time, soap can take advantage of the XML namespace, which is not necessary for XML-RPC. As a result, the beginning of a SOAP message can be any type of XML namespace declaration, at the cost of adding more complexity and incompatibility between systems.

With the awakening of the computer industry and the discovery of the commercial potential of xml-based Web services, companies are beginning to tap into ideas, ideas, arguments, and standardized attempts. The consortium has managed to organize the results exhibition in the name of "Web service Activity", which also includes the XML protocol workgroup (XML Protocol Working Group), which actually makes soap. The number of standardized results associated with Web services (in some ways soap-related or soap-dependent) has multiplied to a surprising extent.

Initially, SOAP developed as an extension of XML-RPC, and it focused on remote procedure calls through the method and variable names obtained from the WSDL file. Now, by making progress, people are discovering more ways to use soap than just "file"-basically using a SOAP envelope to transfer XML format files. In any case, to master soap, it is essential to understand the role that WSDL plays.

Second, SOAP packet structure analysis

SOAP messages are called a soap Envelope, including the SOAP header and SOAP body. Among them, the SOAP header can easily insert various other messages to expand the functionality of the Web service, such as security (with a certificate access Web service), SOAP body is the specific message text, that is, after the Marshall information.

When soap is invoked, that is, sending an HTTP POST message to a URL (such as HTTP://API.GOOGLE.COM/SEARCH/BETA2) (the HTTP GET message can also be supported according to the SOAP specification), and the name of the calling method is called in the HTTP The Request Header is given in Soap-action, followed by the SOAP envelope. The service receives the request, performs the calculation, Marshall the returned result into XML, and returns it to the client with HTTP.

Three, Soap simple example

SOAP development generally has three ways to choose:

1), Pear's own SOAP extension;
2), PHP with the SOAP extension;
3), Nusoap (pure PHP).

The built-in SOAP extensions are added to PHP 5 and are provided as part of PHP so there is no need to download, install, and manage individual packages. This is the first SOAP implementation written for PHP in C instead of PHP, so the authors claim it is much faster. Related documentation is included in the Function Reference section of the PHP Manual (Php_soap.dll).

An example of a client accessing a. NET Web service:

<? php
$objSoapClient = new SoapClient ("Http://www.webservicemart.com/uszip.asmx?WSDL");
$param = Array ("ZipCode" => ' 12209 '); 
$out = $objSoapClient->validatezip ($param);
$data = $out->validatezipresult;
echo $data;
? >

Iv. examples

1), using PHP to establish a SOAP service

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

<? PHP/** * A Simple Math Utility class * * math{/** * ADD two integers together * * @param integer $a the FIR St Integer of the addition * @param integer $b The second integer of the addition * @return integer the sum of the pro
  vided integers * * Public function Add ($a, $b) {return $a + $b; }/** * Subtract two integers from all other * * @param integer $a the ' the ' subtraction * @para M integer $b The second integer of the subtraction * @return integer The difference of the provided integers * * Publ
  IC function sub ($a, $b) {return $a-$b; }/** * Div two integers from each other * * @param integer $a the ' the ' subtraction * @param int Eger $b The second integer of the subtraction * @return double the difference of the provided integers * * Public fun
    Ction Div ($a, $b) {if ($b = = 0) {throw new SoapFault ( -1, "Cannot divide by zero!");
  return $a/$b; }} $server = new SOapserver (' math.wsdl ', Array (' Soap_version ' =>soap_1_2));
$server->setclass ("math"); 
$server->handle ();

 ?>

Note:

A), the math class is about to open webservice;
b), $server->setclass, not $server->addclass.
2, with PHP Client access just to establish a SOAP service

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

Essentially, Http://localhost/php/soap/soap_server.php?WSDL is to access the WSDL descriptor that the comment line refers to, so the WSDL file must be generated beforehand. For other languages such as Java, it can be generated dynamically. For PHP's own SOAP extensions, this WSDL file must be built in advance.

A static WSDL file can be generated using Zendstudio, and the Phpdoc of the math class is used as the metadata for generating the WSDL. When generating a WSDL file with Zendstudio, the Web service destination address must be properly described and the fragment is as follows:

...
  <service name= "MathService" >
    <port binding= "typens:mathbinding" name= "Mathport" >
      <soap: Address location= "http://localhost/php/soap/soap_server.php" ></soap:address>
    </port>
  </service> ...


Note: The method that invokes the PHP webserver must pass in the named parameter.

More interested in PHP related content readers can view the site topics: "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document Skills Summary (including word,excel,access, PPT), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Course", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.