PHP uses SOAP extensions to implement WebService methods, soapwebservice_php tutorial

Source: Internet
Author: User
Tags getting started with php wsdl

PHP uses SOAP extensions to implement the WebService method, Soapwebservice


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

Recently in a PHP project docking external interface involves webservice, search engine related articles are not many, found mostly refers to a very powerful open source software nusoap (download address: http://sourceforge.net/projects/ nusoap/), that is, some classes. The article describes the environment is PHP 4.3, is now popular PHP 5.2 or PHP 5.3. Try it first, run an error, the original NUSOAP provides the SoapClient class and PHP 5 added a built-in SOAP extension soapclient class conflict.

Although Nusoap claims to be available for all PHP environments, it is not affected by server security settings. But the need to refer to a large number of class files, or feel that the new built-in SOAP extension in PHP 5 is better, to achieve the practical good. Let's look at soap:

One, soap and XML-PRC comparison

In the early days of the development of Web services, the first major use of XML format messages was to apply to the XML-RPC protocol, where RPC represented a remote procedure call. In an XML remote procedure call (XML-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 number of data types and some simple data structures. It was thought that the deal was not strong enough, so there was soap--. Its initial definition is a Simple Object access protocol. After that, it was gradually realized that soap was not simple, and that there was no need to use object-oriented language, so now people are just using the name soap.

XML-RPC has only a simple set of data types, and instead, SOAP defines data types by leveraging the evolving XML schema. At the same time, soap can take advantage of XML namespaces, which are not needed by 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 the systems.

With the awakening of the computer industry, the commercial potential of XML-based Web services has been discovered, and companies are beginning to explore ideas, opinions, arguments, and standardized attempts. The World Expo has tried to organize the results exhibition in the name of "Web service Activity", which also includes the XML Protocol Working Group (XML Protocol Working Group) that actually made soap. The number of standardized results related to Web services, to some extent soap-related or soap-dependent, has multiplied to a surprising degree.

Initially, SOAP was developed as an extension of the XML-RPC, and it was primarily focused on remote procedure calls by means of methods and variable names obtained from the WSDL file. Now, by making progress, people have found more ways to use soap, not just "file"-basically using a SOAP envelope to send XML format files. In any case, to master soap, it is essential to understand the role that WSDL plays.

Second, SOAP packet structure parsing

The SOAP message is called a soap Envelope, including the SOAP header and the soap Body. In this case, the SOAP header can easily insert various other messages to extend the functionality of the Web service, such as security (using a certificate to access the Web service), the SOAP body is the specific message body, that is, the information after Marshall.

At the time of soap invocation, that is, sending an HTTP POST message to a URL (such as HTTP://API.GOOGLE.COM/SEARCH/BETA2) (which is also supported by the SOAP specification), invoking the name of the method in the HTTP The Request Header is given in Soap-action, and the next is SOAP envelope. The service receives the request, performs the calculation, Marshall the returned result into XML, and returns it to the client using HTTP.

Third, Soap Simple example

There are generally three ways to choose SOAP Development:

1), pear with the SOAP extension;
2), PHP comes with the SOAP extension;
3), Nusoap (pure PHP).

PHP 5 Adds a built-in SOAP extension that is provided as part of PHP, so there is no need to download, install, and manage individual packages. This is the first SOAP implementation to be written in C instead of PHP, so the author claims it is much faster. The relevant documentation is contained 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), build SOAP service with PHP

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

<? php/*** A Simple Math Utility class*/class math{/** * Add a integers together * * @param integer $a the first Integ Er of the addition * @param integer $b The second integer of the addition * @return integer The sum of the provided inte  Gers */Public function Add ($a, $b) {return $a + $b; }/** * Subtract-integers from each other * * @param integer $a the first integer of the subtraction * @param int Eger $b The second integer of the subtraction * @return integer The difference of the provided integers * * Public funct  Ion Sub ($a, $b) {return $a-$b; }/** * Div. Integers from all 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 di    V ($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 be public webservice;
b), $server->setclass, not $server->addclass.
2), with PHP client access just built 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: $r Esult ";} catch (SoapFault $e) {  print "Sorry An error is caught executing your request: {$e->getmessage ()}";}? >

Essentially, the Http://localhost/php/soap/soap_server.php?WSDL is to access the WSDL description file referred to in the comment line, so the WSDL file must be generated beforehand. For other languages such as Java, they can be generated dynamically. For a SOAP extension that comes with PHP, this WSDL file must be generated beforehand.

You can use Zendstudio to generate a static WSDL file, where the Phpdoc of the math class is used as the metadata to generate the WSDL. When generating a WSDL file with Zendstudio, the Web service destination address must be correctly described, with the following fragment:

...  
 
      
  
         
   
        
  
     
 
  ...

Note: Methods that call PHP webserver must pass in named parameters.

More readers interested in PHP related content can view this site: "Summary of PHP operations and operator usage," PHP Network Programming Tips Summary, "PHP Basic Grammar Primer Tutorial", "PHP operation Office Document tips summary (including word,excel,access, ppt), "PHP Date and Time usage summary", "PHP primer for Object-oriented programming", "PHP String Usage Summary", "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • PHP uses SOAP to call. NET WebService data
    • How PHP uses Nusoap to invoke Web services
    • How to implement SOAP communication in PHP
    • Example of SOAP usage in PHP
    • Reseal PHP code to implement HTTP connection security authentication Zend_soap
    • PHP XML Error parsing SOAP payload on line 1
    • PHP WebService simple example of calling. Net
    • Simple examples and implementation steps for PHP implementation WebService
    • PHP Implementation WebService Instance
    • Example of creating and invoking WebService interfaces in PHP
    • PHP calls Java's WebService simple instance
    • How PHP calls WebService application Introduction
    • Using WSDL to create standard WebService implementation code in PHP

http://www.bkjia.com/PHPjc/1117043.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117043.html techarticle PHP uses SOAP extensions to implement WebService methods, Soapwebservice This article describes how PHP implements WebService using SOAP extensions. Share to everyone for your reference, as follows: the most ...

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