Home  >  Article  >  Backend Development  >  XML-RPC Constructing Web Service in PHP_PHP Tutorial

XML-RPC Constructing Web Service in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:35:27749browse

PHP中集成了XML-RPC和SOAP两种协议的访问,都是集中在xmlrpc扩展当中。另外,在PHP的PEAR中,不管是PHP 4还是PHP 5,都已经默认集成了XML-RPC扩展,而且该扩展跟xmlrpc扩展无关,能够独立实现XML-RPC的协议交互,如果没有xmlrpc扩展,建议使用PEAR::XML-RPC扩展。

Web Service介绍

Web Service就是为了异构系统的通信而产生的,它基本的思想就是使用基于XML的HTTP的远程调用提供一种标准的机制,而省去建立一种新协议的需求。目前进行Web Service通信有两种协议标准,一种是XML-RPC,另外一种是SOAP。XML-RPC比较简单,出现时间比较早,SOAP比较复杂,主要是一些需要稳定、健壮、安全并且复杂交互的时候使用。

我们这里主要是以XML-RPC来简单描述Web Service的交互过程,部分内容来自PHP手册,更详细内容,建议参考手册。

安装xmlrpc扩展

如果你的系统中没有安装xmlrpc的php扩展,那么请正确安装。

在Windows平台下,首先把PHP安装目录下的扩展php_xmlrpc.dll放到C:Windows或者C:Winnt目录下,(PHP4的扩展在C:phpextensions目录中,PHP5的扩展在C:phpext目录中),同时在C:Windowsphp.ini或者C: Winntphp.ini中把extension=php_xmlrpc.dll前面的分号";"去掉,然后重启Web服务器后查看 phpinfo()有没有XML-RPC项目就能够确定是否已经正确安装xmlrpc扩展。

在Unix/Linux平台下,如果没有安装xmlrpc扩展,请在重新编译PHP,在configure的时候请加入 --with-xmlrpc 选项,然后查看phpinfo()看是否正常安装xmlrpc。

(注意:以下操作都是建立在xmlrpc扩张正常安装前提下,请务必正确安装。)

XML-RPC工作原理

XML-RPC大致就是整个过程就是使用XML来进行通信。首先构造一个RPC 服务器端用来出来从RPC客户端传递过来的使用XML封装的请求,并且把处理结果通过XML的形式返回给RPC客户端,客户端就去分析XML获取自己需要的数据。

XML-RPC的服务器端必须有现成的函数提供给客户端调用,并且客户端提交的请求中的函数和方法必须和服务器端的一致,否则将无法获取所需要的结果。

下面我进行简单的代码来描述整个过程。

XML-RPC实践

服务器端使用xmlrpc_server_create函数产生一个服务器端,然后把需要需要暴露的RPC调用接口进行注册,接受RPC客户端POST过来的XML数据,然后进行处理,处理结果通过XML的形式显示给客户端。

代码如下: rpc_server.php

/**
* 函数:提供给RPC客户端调用的函数
* 参数:
* $method 客户端需要调用的函数
* $params 客户端需要调用的函数的参数数组
* 返回:返回指定调用结果
*/
function rpc_server_func($method, $params) {
$parameter = $params[0];
if ($parameter == "get"){
$return = This data by get method;
}else{
$return = Not specify method or params;
}
return $return;
}

//产生一个XML-RPC的服务器端
$xmlrpc_server = xmlrpc_server_create();

//注册一个服务器端调用的方法rpc_server,实际指向的是rpc_server_func函数
xmlrpc_server_register_method($xmlrpc_server, "rpc_server", "rpc_server_func");

//接受客户端POST过来的XML数据
$request = $HTTP_RAW_POST_DATA;

//执行调用客户端的XML请求后获取执行结果
$xmlrpc_response = xmlrpc_server_call_method($xmlrpc_server, $request, null);

//把函数处理后的结果XML进行输出
header(Content-Type: text/xml);
echo $xmlrpc_response;

//销毁XML-RPC服务器端资源
xmlrpc_server_destroy($xmlrpc_server);
?>

服务器端构造好了,那么再构造我们的RPC客户端。客户端大致通过Socket访问XML-RPC服务器端的80端口,然后把需要调用的RPC接口封装到XML里,通过POST请求提交给RPC服务器端,最后获取服务器端返回结果。

代码如下:rpc_client.php

/**
* 函数:提供给客户端进行连接XML-RPC服务器端的函数
* 参数:
* $host 需要连接的主机
* $port 连接主机的端口
* $rpc_server XML-RPC服务器端文件
* $request 封装的XML请求信息
* 返回:连接成功成功返回由服务器端返回的XML信息,失败返回false
*/
function rpc_client_call($host, $port, $rpc_server, $request) {

//打开指定的服务器端
$fp = fsockopen($host, $port);

//Construct the query POST request information of the XML-RPC server that needs to communicate
$query = "POST $rpc_server HTTP/1.0 User_Agent: XML-RPC Client Host: ".$host." Content-Type: text/xml Content-Length: ".strlen($request)." ".$request." ";

//Send the constructed HTTP protocol to the server, and return false on failure
if (!fputs($fp, $query, strlen($query))) {
$errstr = "Write error" ;
return false;
}

//Get all information returned from the server, including HTTP headers and XML information
$contents = ;
while (!feof($ fp)){
$contents .= fgets($fp);
}

//Return the obtained content after closing the connection resource
fclose($fp);
return $contents;
}

//Construct information to connect to the RPC server
$host = localhost;
$port = 80;
$rpc_server = /~heiyeluren/rpc_server.php;

//Encode the XML request that needs to be sent into XML. The method that needs to be called is rpc_server, and the parameter is get
$request = xmlrpc_encode_request(rpc_server, get);

//Call the rpc_client_call function to send all requests to the XML-RPC server and obtain the information
$response = rpc_client_call($host, $port, $rpc_server, $request);

//Analyze the XML returned from the server, remove the HTTP header information, and convert the XML into a string that PHP can recognize
$split = ;
$xml = explode($split, $response);
$xml = $split . array_pop($xml);
$response = xmlrpc_decode($xml);

//Output the information obtained from the RPC server
print_r($response);
?>

Roughly speaking, our above example is to submit a method called rpc_server, the parameter is get, and then get the return from the server. The XML data returned by the server is:


value>
This data by get method





Then we encode this XML into a PHP string through the xmlrpc_decode function, and we can process it at will, and the entire Web Service interaction is completed.

Conclusion

Whether it is XML-RPC or SOAP, as long as it allows us to make remote process calls stably and safely and complete our project, then the entire Web Service will be successful. In addition, if possible, you can also try to use XML-RPC in PEAR to implement similar operations above. It may be simpler and more suitable for you.

Simply use XML-RPC for Web Service interaction. Please refer to the PHP manual for part of the code. If you want to get detailed information, it is recommended to refer to the manual. If the article is incorrect, please correct me.

This article comes from PHP Information Original link: html/84/n-33884.html">http://www.phpchina.com/html/84/n-33884.html


http://www.bkjia.com/PHPjc/508344.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508344.htmlTechArticlePHP integrates access to two protocols, XML-RPC and SOAP, both of which are concentrated in the xmlrpc extension. In addition, in PHP's PEAR, whether it is PHP 4 or PHP 5, the XML-RPC extension has been integrated by default...
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