This article mainly introduces examples of creating and calling webservice interfaces in php, including basic knowledge of webservice, webservice server example, and webservice client example. if you need a friend, refer to this document as a developer, to write a webservice interface or call someone else's webservice interface, you must first understand what is webservice. Simply put, WebService is an open Service for some sites, or a self-developed Service, that is, some methods. you can specify a method name through the URL to send a request, the service (method) in the site receives your request, performs some processing based on the passed parameters, and then returns the processed results to you in XML format, your program parses the XML data and displays it or performs other operations.
To write a webservice, you must understand that the basic Web Services platform is XML + HTTP. In addition, the elements of the Web services platform are SOAP (Simple Object Access Protocol), UDDI (general description, discovery, and integration ), WSDL (Web services Description Language); any webservice includes the client and server. The following example shows how to use php to write webservice interfaces for others to call:
First, we need to create a. wsdl file. how can we create this file in php. There are two ways to achieve this: one is to directly use zend studio tools to generate; the other is php based on SoapDiscovery. class. php automatically generates the wsdl file. Which of the following methods can be selected based on your own situation? I usually use the former method as quickly as possible. Next, let's write how to generate a wsdl file using a class. first, download the class file online, and then introduce the class file. See the following code:
Creat_wsdl.php
The code is as follows:
<? Php
Include_once ('service. php ');
Include_once ('soapdiscovery. class. php ');
$ Wsdl = new SoapDiscovery ('service', 'Soap '); // The first parameter is the class name, which is also the name Service of the generated wsdl file. wsdl. The second parameter is the service name.
$ Wsdl-> getWSDL ();
?>
In this way, run the creat_wsdl.php file to generate the wsdl file. Is it easy?
Any webservice must be bound to an implementation class. That is to say, the actual function of the wsdl file called by others is the method in the implementation class. the following code is a server class file.
Service. php
The code is as follows:
<? Php
Class Service
{
Public function Hello ()
{
Echo 'hello good ';
}
Public function Add ($ a, $ B)
{
Return $ a + $ B;
}
}
$ Server = SoapServer ('service. php', array ('soap _ version' => soap_1_2 ));
$ Server-> setClass ('service'); // registers all methods of the Service class
$ Server-> handle (); // process the request
?>
After writing the server and the wsdl file, the client needs to call it. See the client call code:
Client. php
The code is as follows:
<? Php
Ini_set ('soap. wsdl_cache_enabled ', '0'); // disable caching
$ Soap = new SoapClient ('http: // 127.0.0.1/soap/Service. php? Wsdl ');
Echo $ soap-> Add (1, 2 );
// Echo $ soap-> _ soapCall ('Add', array (1, 2) // or call
?>
This is a complete example code for writing webservice interfaces and calling. it is helpful for the required phper;
The webservice interface of others is called as the code written by client. php.