Implementing Web Service with PHP soap

Source: Internet
Author: User
Tags soap wsdl

PHP has two extensions to implement Web service, one is Nusoap, one is the official PHP SOAP extension, because soap is official, so here we use SOAP to implement the Web Service. Because the SOAP extension is not turned on by default, let's see if the SOAP extension is open.

In the process of writing Web service by soap, the Soapclient,soapserver,soapfault three classes are used primarily.

SoapClient class

This class is used for Web services. The SoapClient class can be a client for a given Web services.
It has two forms of operation:

* WSDL mode

* NON-WSDL mode

In WSDL mode, the constructor can use the WSDL file name as a parameter and extract the information used by the service from the WSDL.

Use parameters in NON-WSDL mode to pass the information that you want to use.

SoapServer class

This class can be used to provide Web services. Similar to SoapClient, SoapServer also has two modes of operation: WSDL mode and NON-WSDL mode. The meaning of these two patterns is the same as the two modes of soapclient. In WSDL mode, the service implements the interface provided by the WSDL, and in non-wsdl mode, the parameters are used to manage the behavior of the service.

Among the many methods of the SoapServer class, there are three methods that are more important. They are Soapserver::setclass (), Soapserver::addfunction (), and Soapserver::handle ().

Here's an example:
Defines a service-serving PHP class that provides a function that the Web service provides externally


<?phpclass personinfo{/** * Return name * @return String * */Public Function getName () {    Return "My Name is chance"; }}?>


The server-side code is given below:

<?php//contains classes providing services come in require_once (' personinfo.php ');    The WSDL method provides a Web service, and if a WSDL file is generated it can be passed directly to the constructor of//soapserver//$s = new soapserver (' personinfo.wsdl '); Doesn ' t work only location cannot provide Web service//output:looks like we got no XML document//$s = new SoapServer (Null,array ("Lo   cation "=" http://localhost/Test/MyService/Server.php "));    The following two ways can work as long as the corresponding URI//$s = new SoapServer (Null,array ("uri" = "server.php") is specified; $s = new SoapServer (Null,array ("Location" = "http://localhost/Test/MyService/Server.php", "uri" = "server.php"     ));    $s-setclass ("PersonInfo"); $s-handle ();? >


Here is the client code:

  <?php  try{      //wsdl method Call web service   &NBSP;&NBSP;&NBSP;&NBSP;//WSDL mode because the WSDL file is written, if the addition of delete functions, such as operation changes, will not reflect the WSDL, the relative non-wsdl way        //is not flexible enough       //$soap  = new soapclient ("http/ LOCALHOST/TEST/MYSERVICE/PERSONINFO.WSDL ");             //non-wsdl method Call web service          // The option location system must be provided in the NON-WSDL mode, and the location of the server is optional and can not provide       $soap  =  new soapclient (Null,array (' location ' = ' http://localhost/Test/MyService/Server.php ', ' uri ' = ' = ') Server.php '),           //two invocation methods, calling the method directly, and using __soapcall to simply call       $result 1 =  $soap->getname ();      $result 2  =  $soap->__soapcall ("GetName",Array ());     echo  $result 1. " <br/> ";     echo  $result 2;      }catch ( soapfault  $e) {     echo  $e->getmessage ()  }catch (Exception  $e) {     echo  $e->getmessage (); }  ?>





Implementing Web Service with PHP soap

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.