Implementing Web Service with PHP soap

Source: Internet
Author: User
Tags getmessage soap php class 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

<? PHP Class personinfo{    /* * *    return name     *    @return      String     **/ public    function  getName () {        return ' My Name is chance ' ;    }}? >

The server-side code is given below:

<?PHP//contains classes that provide 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 ("Loca  tion "=" 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=NewSoapServer (NULL,Array("Location" = "http://localhost/Test/MyService/Server.php", "uri" = "server.php")); $sSetClass ("PersonInfo"); $s-handle ();?>

Here is the client code:

<?PHPTry{     //WSDL method called in the Web service//WSDL mode because the WSDL file is written, if the addition of delete functions, such as operation changes, does not reflect the WSDL, relative NON-WSDL mode//is not flexible//$soap = new Soa          Pclient ("http://localhost/Test/MyService/PersonInfo.wsdl"); The NON-WSDL method calls the Web service//In the NON-WSDL mode that the option location system must provide, while the service side is optional and can not provide     $soap=NewSoapClient (NULL,Array(' Location ' = ' http://localhost/Test/MyService/Server.php ', ' uri ' = ' server.php ')); //Two call methods, call the method directly, and use __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 ();} ?>

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.