Implement simple Web Services using PHP SOAP extension_PHP tutorial

WBOY
Release: 2016-07-13 10:21:24
Original
941 people have browsed it

Using PHP SOAP extension to implement simple Web Services

What can WebServices do?
WebServices can convert applications into web applications.
By using WebServices, your application can publish information to the world, or provide a certain function.
Okay, there is a lot of information about WebServices online, so I won’t introduce too much and go directly to the topic.
PHP has two extension libraries that can implement WebServices, one is NuSoap, and the other is the official Soap extension that comes with PHP. They are roughly the same in use. Take the official Soap extension as an example.
The three classes SoapClient, SoapServer, and SoapFault are mainly used in writing WebServices in Soap.
SoapClient: The class that users access, that is, the client, uses WebServices classes
SoapServer: Provides WebServices class, server side
SoapFault: Exception handling class
As an example, let’s write a super simple WebServices to get a feel for it, let’s talk directly in code
1. Myself.class.php --Business logic class, function implementation class
Copy code
1
2 //Business logic class
3 class Myself{
4 public function info(){
5 return "Sina Weibo: Balla_Rabbit, please pay attention~";
6 }
7 }
8 ?>
Copy code
It’s very simple, no need for extra explanation, it returns a string.
2. soapServer.php - Server class, providing services
Copy code
1
2 //Server
3 require_once('Myself.class.php');
4 $parameter=array(
5 'uri'=>'http://localhost/',
6 'location'=>'http://localhost/soap/soapServer.php'
7 );
8 $soapServer=new SoapServer(null,$parameter);
9 $soapServer->setClass('Myself');
10 $soapServer->handle();
11 ?>
Copy code
SoapServer has two operating modes:
The above example is non-WSDL mode. When instantiating the SoapServer class, one parameter is to put the WSDL file. In non-WSDL mode, it can be empty, and the configuration parameters are written in the form of an array in the second parameter. .
If you are using WSDL mode, you can directly use the WSDL file to let the server read the configuration parameters. In this case, you can omit the second array parameter.
There are many configuration parameters. Only 2 are listed above for simple examples. You can check the details online
uri --namespace
location --service address
1. 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.
2. Non-WSDL mode In the non-WSDL mode, parameters are used to pass the information to be used to manage the behavior of the service.
Among the many methods of the SoapServer class, three methods are more important. They are SoapServer::setClass(), SoapServer::addFunction(), SoapServer::handle().
Special attention should be paid to the fact that no parameters can be output before or after the handle method, otherwise an error will occur.
3. soapClient.php --Client class, using service
Copy code
1
2 //Client
3 $parameter=array(
4 'uri'=>'http://localhost/',
5 'location'=>'http://localhost/soap/soapServer.php'
6 );
7 try{
8 $soapClient=new SoapClient(null,$parameter);
9 echo $soapClient->info();
10
11 }catch(Exception $e){
12 echo $e->getMessage();
13 }
14
15 ?>
Copy code
The SoapClient class can serve as a client for given WebServices.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/859801.htmlTechArticleUsing PHP SOAP extension to implement simple Web Services What can WebServices do? WebServices can convert applications into web applications. By using WebServices, your application can...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template