Example of creating and calling webservice interface in php

  • 2021-07-09 07:34:05
  • OfStack

As a developer, if you want to write webservice interface or call someone else's webservice interface, you first need to know what webservice is. To put it simply, WebService means that one site opens one service. Can also be your own development of Service, that is, a number of methods, through URL, specify a method name, send a request, the site of this service (method), receive your request, according to the parameters, do a number of processing, and then the results of processing in the form of XML back to you, your program will analyze these XML data, and then display or do other operations.

To write webservice, you need to know that the basic Web Services platform is XML + HTTP; In addition, the elements of Web services platform: SOAP (Simple Object Access Protocol), UDDI (Universal Description, Discovery and Integration), WSDL (Web services description language); Any webservice includes a client and a server. Here is an example of how to write an webservice interface with php for others to call:

First, you need to build a. wsdl file, so how does php build this file? There are two ways to achieve, one is directly generated by zend studio tool; The other is that php automatically generates wsdl files according to SoapDiscovery. class. php; Specific which one according to their own situation to choose, I use the former so fast. Write below 1 how to generate wsdl file with class, first need to download that class file on the Internet, and then introduce the class file, look at the following code:
creat_wsdl.php

<?php
include_once('Service.php');
include_once('SoapDiscovery.class.php');
$wsdl=new SoapDiscovery('Service','soap');// No. 1 1 Parameter is the class name, which is also generated wsdl File name of Service.wsdl , No. 2 The parameter is the name of the service. You can write it at will
$wsdl->getWSDL();
?>

Running the creat_wsdl. php file generates the wsdl file. Isn't it very simple
Any 1 webservice needs to be bound to 1 implementation class. That is to say, the wsdl file called by others actually really plays a role in realizing the methods in the class; The following code is the server-side class file
Service.php
<?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');// Registration Service All methods of the class
$server->handle();// Processing requests
?>

After writing the server and wsdl files, it is necessary for the client to call. See the client calling code:

client.php

<?php
ini_set('soap.wsdl_cache_enabled','0');// Turn off the cache
$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 it can be called like this
?>

This is a complete write webservice interface and call the instance code, I hope the need for phper help;
Then calling someone else's webservice interface is the code written by client. php.


Related articles: