Original: Implementing Web Service with PHP soap
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
1<?PHP
2ClassPersonInfo
3{
4 /**
5* Return name
6* @return String
7 *
8 */
9 Public functionGetName () {
Ten return "My Name is chance";
One }
A}
?>
The server-side code is given below:Code
1 <?PHP
2//contains classes that provide services come in
3require_once('personinfo.php');
4
5//WSDL provides a Web service that can be passed directly to the//soapserver constructor if a WSDL file is generated
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 document
Ten//$s = new SoapServer (Null,array ("Location" = "http://localhost/Test/MyService/Server.php"));
One
A//The following two ways can work, as long as the corresponding URI is specified
-//$s = new SoapServer (Null,array ("uri" = "server.php"));
- $s = NewSoapServer (NULL,Array(" Location"="http://localhost/Test/MyService/Server.php","URI"="server.php"));
the
-$s -SetClass ("PersonInfo");
-
-$s -handle ();
+?>
Here is the client code:
Code
1<?PHP
2Try{
3 //The WSDL method calls the Web service
4//wsdl mode because the WSDL file is written, if the addition of delete functions, such as operation changes, does not reflect the WSDL, the relative non-wsdl way
5//Not flexible enough
6//$soap = new SoapClient ("http://localhost/Test/MyService/PersonInfo.wsdl");
7
8//non-wsdl method calls Web service
9//In the NON-WSDL mode, the option location system must provide, and the service-side is optional and may not provide
Ten $soap = NewSoapClient (NULL,Array(' Location'="http://localhost/Test/MyService/Server.php",'URI'='server.php'));
One
A //Two call methods, call the method directly, and use __soapcall to simply call
- $result 1 = $soap -GetName ();
- $result 2 = $soap -__soapcall ("GetName",Array());
the Echo $result 1."<br/>";
- Echo $result 2;
-
-}Catch(SoapFault$e){
+ Echo $e -GetMessage ();
-}Catch(Exception $e){
+ Echo $e -GetMessage ();
A}
at
-?>
Implementing Web Service with PHP soap