Home > Backend Development > PHP Tutorial > PHP creates and calls webservice interface sample code

PHP creates and calls webservice interface sample code

怪我咯
Release: 2023-03-13 20:08:01
Original
9173 people have browsed it

Introduction to webservice

Web service is a platform-independent, low-coupling, self-contained, web-based application that can be programmed and can be used The open XML (a subset of Standard Generalized Markup Language) standard to describe, publish, discover, coordinate and configure these applications is used to develop distributed interoperable applications.

Web Service technology enables different applications running on different machines to exchange data or integrate with each other without the need for additional, specialized third-party software or hardware. Applications implemented according to Web Service specifications can exchange data with each other regardless of the language, platform or internal protocol they use. Web Service is a self-describing, self-contained available network module that can perform specific business functions. Web Services are also easy to deploy because they are based on some conventional industry standards and existing technologies, such as subsets of XML and HTTP under the standard universal markup language. Web Services reduce the cost of application interfaces. Web Service provides a common mechanism for the integration of business processes across the entire enterprise or even between multiple organizations.

This article mainly introduces examples of creating and calling webservice interfaces in PHP, including basic knowledge of webservice, webservice server examples, and webservice client examples. Friends in need can refer to

Writing webservice Need to know: The basic Web Services platform is XML + HTTP; in addition, the elements of the Web services platform: SOAP (SimpleObjectAccess Protocol), UDDI (Universal Description, Discovery and Integration), WSDL (Web Services Description Language ); Any webservice includes a client and a server. The following uses examples 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 does PHP create this file. 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 the 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

The code is as follows:

<?php
include_once(&#39;Service.php&#39;);
include_once(&#39;SoapDiscovery.class.php&#39;);
$wsdl=new SoapDiscovery(&#39;Service&#39;,&#39;soap&#39;);//第一参数为类名,也是生成wsdl的文件名Service.wsdl,第二个参数是服务的名字可以随便写
$wsdl->getWSDL();
?>
Copy after login

Run the creat_wsdl.php file in this way to generate the wsdl file. Is 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

The code is as follows:

<?php
class Service
{
public function Hello()
{
echo &#39;hello good&#39;;
}
public function Add($a,$b)
{
return $a+$b;
}
}
$server=SoapServer(&#39;Service.php&#39;,array(&#39;soap_version&#39;=>soap_1_2));
$server->setClass(&#39;Service&#39;);//注册Service类的所有方法
$server->handle();//处理请求
?>
Copy after login

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

client.php

The code is as follows:

<?php
ini_set(&#39;soap.wsdl_cache_enabled&#39;,&#39;0&#39;);//关闭缓存
$soap=new SoapClient(&#39;http://127.0.0.1/soap/Service.php?wsdl&#39;);
echo $soap->Add(1,2);
//echo $soap->_soapCall(&#39;Add&#39;,array(1,2))//或者这样调用也可以
?>
Copy after login

This is a complete example code for writing the webservice interface and calling it. I hope It will be helpful for the phper that needs it;
Then calling other people’s webservice interface is the code written by client.php.

The above is the detailed content of PHP creates and calls webservice interface sample code. For more information, please follow other related articles on the PHP Chinese website!

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