Home  >  Article  >  Backend Development  >  Example of creating and calling webservice interface in PHP_PHP tutorial

Example of creating and calling webservice interface in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:21:49732browse

Examples of creating and calling webservice interfaces in php

This article mainly introduces examples of creating and calling webservice interfaces in php, including basic knowledge of webservice, webservice server examples, webservice clients Here is an example, friends who need it can refer to it

As a developer, if you want to write a webservice interface or call someone else's webservice interface, you first need to understand what a webservice is. To put it simply, WebService is a service opened by some sites, or it can be a Service developed by you, that is, some methods. Through the URL, you specify a method name and make a request. The service (method) in the site will receive your request. The request will do some processing based on the passed parameters, and then return the processed results to you in XML form. Your program will parse the XML data and then display it or perform other operations.

You need to understand when writing webservices: the basic Web Services platform is XML + HTTP; in addition, the elements of the Web services platform: SOAP (Simple Object Access Protocol), UDDI (Universal Description, Discovery and Integration), WSDL (Web Services Description Language) ;Any webservice includes client and server. The following is an example to explain how to use PHP to write a webservice interface for others to call:

First you need to create a .wsdl file, so how to create this file in PHP. There are two ways to achieve this. One is to generate it directly using the zend studio tool; the other is to use PHP to automatically generate a wsdl file based on SoapDiscovery.class.php. Which one you choose depends on your own situation. I generally use the former. This comparison fast. Let’s write down how to generate a wsdl file using a class. First, you need to download the class file from the Internet, and then after importing the class file, look at the following code:

creat_wsdl.php

Copy the code. The code is as follows:

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 file name of generated wsdl Service.wsdl, the second parameter is the name of the service, you can write it casually

 $wsdl->getWSDL();

 ?>

In this way, the wsdl file can be generated by running the creat_wsdl.php file. Isn’t it very simple

Any webservice needs to be bound to an implementation class. In other words, the real function of the wsdl file called by others is to implement the methods in the class; the following code is the server class file

Service.php

Copy the code. The code is as follows:

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');//Register all methods of the Service class

 $server->handle();//Processing the request

 ?>

After writing the server and wsdl files, you need to call them from the client. Please see the client calling code:

client.php

Copy the code. The code is as follows:

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 you can call it like this

 ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/852814.htmlTechArticleExamples of creating and calling webservice interfaces in php. This article mainly introduces examples of creating and calling webservice interfaces in php, including Basic knowledge of webservice, webservice server examples, webser...
Statement:
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