Using PHP soap to implement Web service[reprint]

Source: Internet
Author: User
Tags getmessage 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: Define a PHP class that provides services that are provided by a Web service.

<?phpclass personinfo{    /**     *    return name     *    @return String * */public    function getName () {        return "My Name is Chance";    }}? >

Server-side code:

1 <?php 2//contains classes providing services in 3 require_once (' personinfo.php '); 4  5 The//WSDL method provides a Web service, and if a WSDL file is generated it can be passed directly into the//soapserver constructor 6//$s = new soapserver (' personinfo.wsdl '); 7  8 Doesn ' t work only location cannot provide Web service 9//output:looks like we got no XML document10//$s = new SoapServer (Null,array (" Location "=" + "http://localhost/Test/MyService/Server.php")); 11 12//The following two methods can work, as long as the corresponding uri13//$s = new soapserver (Null,array ("uri" = "server.php"); $s = new SoapServer (Null,array ("Location" = "http://localhost/Test/") Myservice/server.php "," uri "=" server.php "), $s, SetClass (" PersonInfo "), $s, handle ();?>

Client code:

1 <?php 2 try{3     //wsdl method call Web Service 4     //wsdl mode because the WSDL file is written, if the add delete function, such as operation changes, does not reflect the WSDL, relative non-wsdl way 5     //Not flexible enough 6     //$soap = new SoapClient ("http://localhost/Test/MyService/PersonInfo.wsdl"); 7      8     // NON-WSDL mode calls Web Service     9     //In non-wsdl mode, the option location system must provide, and the service side is optional, can not provide a     $soap = new SoapClient (Null,array (' location ' = ' http://localhost/Test/MyService/Server.php ', ' uri ' = ' server.php ')); 11     Two call methods, call the method directly, and use __soapcall simple call     $result 1 = $soap->getname ();     $result 2 = $soap->_ _soapcall ("GetName", Array ()),     echo $result 1. " <br/>     echo $result 2;17     }catch (SoapFault $e) {     echo $e->getmessage (),}catch ( Exception $e) {     echo $e->getmessage ();}23?>

Source Address: http://www.cnblogs.com/chance1/archive/2009/04/08/1431949.html

Using PHP soap to implement Web service[reprint]

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.