Home  >  Article  >  Backend Development  >  Example of creating and calling webservice interface in php, webservice example_PHP tutorial

Example of creating and calling webservice interface in php, webservice example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:161079browse

Example of creating and calling webservice interface in php, webservice example

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 uses 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. 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 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, running the creat_wsdl.php file can generate a wsdl 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 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 requests
?>

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

client.php

Copy 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
?>

This is a complete example code for writing webservice interface and calling, I hope it will be helpful to those who need phper;
Then calling someone else's webservice interface is the code written in client.php.

How to call web service interface in php

Use soapclient, it’s not difficult. If you are using Java, it should be no problem! If you have any questions, please contact us in detail!

php calls java's webservice interface

The interface can be called directly; it is best to have an interface description. Parameter type is the key

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/848796.htmlTechArticleExamples of creating and calling webservice interfaces in php, webservice examples As a developer, if you want to write a webservice interface or call For other people's webservice interface, you first need to understand what w...
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