php xmlrpc 第三方工具使用 The Incutio XMLRPC Library for PHP

xml-rpc 是一种使用HTTP协议传输XML格式文件来获取远程程序调用(Remote Procedure Call)的传输方式。

The Incutio XML-RPC Library for PHP   xml-rpc第三方工具,官网http://scripts.incutio.com/xmlrpc/,参考手册:http://scripts.incutio.com/xmlrpc/manual.php

到官网下载最新版本,就是一个IXR_Library.php文件。

使用方法:

1、创建一个客户端:如 testClient.php

<?php
//引入第三方类库。
require_once dirname(__FILE__)."/IXR_Library.php";
//服务端地址。
$server_url = "http://localhost/heping.com/xmlrpc/testServer.php";
//创建客户端对象。
$client = new IXR_Client($server_url);
//debug 调试用,打印出请求和返回结果的详细xml结构。
$client->debug = true;
//请求服务端test.add方法,后两个为方法的参数。服务端的方法在服务端(testServer.php)中实现。
if (!$client->query('test.add', 3, 4)) {
    //产生错误 ,取出错误编号和错误信息。
    die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
} else {
    //输出服务商响应结果。
    echo $client->getResponse();
}
echo "<br>";
//请求服务端不带参数的方法。
if (!$client->query('test.sayHello')) {
    die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
} else {
    echo $client->getResponse();
}

使用debug可以打印出请求和响应的原始数据,如:test.add 的请求数据如下:我们不用去关心这部分数据格式问题,IXR_Client类进行了处理。

POST /heping.com/xmlrpc/testServer.php HTTP/1.0
Host: localhost
Content-Type: text/xml
User-Agent: The Incutio XML-RPC PHP Library
Content-Length: 186

<?xml version="1.0"?>
<methodCall>
<methodName>test.add</methodName>
<params>
<param><value><int>3</int></value></param>
<param><value><int>4</int></value></param>
</params></methodCall>

响应数据如下:$client->getResponse() 方法将下面的结果转换成我们习惯用的数据格式。如字符串或数组。

HTTP/1.1 200 OK
Date: Mon, 10 Dec 2012 06:40:42 GMT
Server: Apache/2.2.17 (Win32) PHP/5.3.3
X-Powered-By: PHP/5.3.3
Connection: close
Content-Length: 153
Content-Type: text/xml

<?xml version="1.0"?>
<methodResponse>
  <params>
    <param>
      <value>
      <int>7</int>
      </value>
    </param>
  </params>
</methodResponse>

2、创建服务端程序,testServer.php

<?php
include dirname(__FILE__)."/IXR_Library.php";

//方法实现
function sayHello($args) {
    return 'Hello!';
}
function addTwoNumbers($args) {
    $number1 = $args[0];
    $number2 = $args[1];
    return $number1 + $number2;
}
//将方法绑定的服务端类里,客户端可直接通过$client->query('test.sayHello');调用到sayHello方法。
$server = new IXR_Server(array(
    'test.sayHello' => 'sayHello',
    'test.add' => 'addTwoNumbers'
));
原文地址:https://www.cnblogs.com/wuheping/p/2811394.html