★ wanayoo — archive 1999 http://fr.php.net/manual/zh/ref.xmlrpc.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<xml_set_unparsed_entity_decl_handlerxmlrpc_decode_request>
view the version of this page
Last updated: Sat, 27 Nov 2004

CXX. XML-RPC 函数库

简介

这些函数可用于编写 XML-RPC 服务端和客户端。你可以在 http://www.xmlrpc.com/ 找到更多关于 XML-RPC 的信息,同时可在 http://xmlrpc-epi.sourceforge.net/ 找到更多关于此扩展及其功能的文档。

警告

本扩展模块是实验性的。该模块的行为,包括其函数的名称以及其它任何关于此模块的文档可能会在没有通知的情况下随 PHP 以后的发布而改变。我们提醒您在使用本扩展模块的同时自担风险。

需求

无需外部库文件就可以加入本扩展模块的支持。

安装

默认情况下在 PHP 中是不能使用 XML-RPC 支持的。你需要使用 --with-xmlrpc[=DIR] 配置选项编译 PHP 才能够使用 XML-RPC 支持。从 PHP 4.1.0 开始附带了此扩展。

运行时配置

这些函数的行为受到全局配置文件 php.ini 的影响。

表格 1. XML-RPC 配置选项

名称默认值作用范围
xmlrpc_errors"0"PHP_INI_SYSTEM
xmlrpc_error_number"0"PHP_INI_ALL
更多关于常量 PHP_INI_* 的细节和解释参见 ini_set()

资源类型

该扩展模块未定义任何资源类型。

预定义常量

该扩展模块未定义任何常量。

目录
xmlrpc_decode_request -- 将 XML 译码为 PHP 本身的类型
xmlrpc_decode -- 将 XML 译码为 PHP 本身的类型
xmlrpc_encode_request -- 为 PHP 的值生成 XML
xmlrpc_encode -- 为 PHP 的值生成 XML
xmlrpc_get_type -- 为 PHP 的值获取 xmlrpc 的类型。对于 base64 与日期时间字符串特别有用。
xmlrpc_parse_method_descriptions -- 将 XML 译码成方法描述的列表
xmlrpc_server_add_introspection_data -- 添加自我描述的文档
xmlrpc_server_call_method -- 解析 XML 请求同时调用方法
xmlrpc_server_create -- 创建一个 xmlrpc 服务端
xmlrpc_server_destroy -- 销毁服务端资源
xmlrpc_server_register_introspection_callback -- 注册一个 PHP 函数用于生成文档
xmlrpc_server_register_method -- 注册一个 PHP 函数用于匹配 xmlrpc 方法名
xmlrpc_set_type -- 为一个 PHP 字符串值设置 xmlrpc 的类型、base64 或日期时间


add a note add a note User Contributed Notes
XML-RPC 函数库
Jerome Delamarche
14-Oct-2004 11:58
The documentation lacks an example that shows how to send a fault in a response. Here is how to do it:

$args = array("faultCode" => $errcode, "faultString" => $errmsg);
$resp = xmlrpc_encode_request(NULL,$args);
//echo $resp;
php at hendrik-krauss dot de
13-Aug-2004 09:38
On "datetime" values:

If you implement an XML-RPC server with these functions and a client calls a method on your server, sending a datetime as parameter (in ISO 8601 format, as specified at http://www.xmlrpc.com/spec), the PHP XML-RPC will pass your registered server method an object as parameter. That object, for example, looks like:
obj->type="datetime"
obj->scalar="20040420T13:32:40"
obj->timestamp=1082460760

If you do xmlrpc_get_type(obj), it will return "datetime", so presumably that function just returns the value of 'type'. 'scalar' seems to be the on-the-wire representation of the datetime (ISO 8601, exactly as received). 'timestamp' appears to be the ISO value in 'scalar' converted into a normal PHP timestamp (i.e. Unix time_t).

Note on 'scalar': Using a MySQL DB, we did something like  "select blah where start_time >= $obj->scalar ;". That actually worked and returned expected results, so MySQL appears to handle that ISO 8601 format correctly.
swunderlin at REMOVE-telemedia dot ch
09-Jun-2004 12:20
pear hs an XML_RPC package, if you can't recompile your php:
http://pear.php.net/package/XML_RPC
steph at zend dot com
08-Jun-2004 01:04
It took me a while to get a client together without external libraries.  This very basic client/server pair works on my home set-up - hopefully it will save the next xml-rpc virgin some grief.

/* clienttest.php */
<?php
function do_call($host, $port, $request) {
  
  
$fp = fsockopen($host, $port, $errno, $errstr);
  
$query = "POST /home/servertest.php HTTP/1.0\nUser_Agent: My Egg Client\nHost: ".$host."\nContent-Type: text/xml\nContent-Length: ".strlen($request)."\n\n".$request."\n";

   if (!
fputs($fp, $query, strlen($query))) {
      
$errstr = "Write error";
       return
0;
   }

  
$contents = '';
   while (!
feof($fp)) {
      
$contents .= fgets($fp);
   }

  
fclose($fp);
   return
$contents;
}

$host = 'localhost';
$port = 80;
$request = xmlrpc_encode_request('cycle', 'egg');
$response = do_call($host, $port, $request);
/* do something with $response, e.g. print it */
?>

/* servertest.php */
<?php
function lifecycle($method, $params) {
/* $method = 'cycle', $params = (array of) request parameter(s); $data is also passed from xmlrpc_server_call_method, if we had any data to pass */
  
switch($params[0]) {
       case
'egg':
          
$reply = 'All eggs will be birds one day.';
       break;
       default:
          
$reply = 'That must have been an otheregg';
   }
   return
$reply;
}

$server = xmlrpc_server_create();

/* register the 'external' name and then the 'internal' name */
xmlrpc_server_register_method($server, "cycle", "lifecycle");

$request = $HTTP_RAW_POST_DATA; // no you don't need 'always on', and no $_POST doesn't work.

/* the parameters here are 'server, xml-string and user data'.  There's supposed to be an optional 'output options' array too, but I can't get it working :( hence header() call */
$response = xmlrpc_server_call_method($server, $request, null);
header('Content-Type: text/xml');
print
$response;

xmlrpc_server_destroy($server);
?>
andrej
25-Feb-2004 05:52
mboeren at php dot net
25-Feb-2004 01:34
Just a quick addition to my previous xmlrpc_client class: since you cannot use remote methods containing capital letters or methods from subhandlers (like 'system.listMethods()'), I added a 'call(...)' method to the class.

<?php
  
// this method should be copy/pasted in the
   // xmlrpc_client class

  
function call($function)
   {
      
$return = NULL;
      
$argv = func_get_args();
      
array_shift($argv); // remove function argument
      
$this->__call($function, $argv, &$return);
       return
$return;
   }

  
// now, you can also do
  
$result = $client->call('system.listMethods');
  
$sum = client->call('add', '1', '2');
?>
mboeren at php dot net
24-Feb-2004 04:01
I use the following code (requires the overload extension) to make developing clients easier:

<?php
include("utils/utils.php"); // from xmlrpc-epi utils

/*
Usage:
   $client = new xmlrpc_client("http://localhost:7080");
   print $client->echo('x')."\n";
   print $client->add(1, 3)."\n";

*/
class xmlrpc_client
{
   var
$url;
   var
$urlparts;

   function
xmlrpc_client($url)
   {
      
$this->url = $url;
      
$this->urlparts = parse_url($this->url);
       foreach(array(
'scheme', 'host', 'user', 'pass', 'path',
                    
'query', 'fragment')
               as
$part) {
           if (!isset(
$this->urlparts[$part])) {
              
$this->urlparts[$part] = NULL;
               }
           }
   }

   function
__call($function, $arguments, &$return)
   {
      
$requestprms['host'] = $this->urlparts['host'];
      
$requestprms['port'] = $this->urlparts['port'];
      
$requestprms['uri'] = $this->urlparts['path'];
      
$requestprms['method'] = $function;
      
$requestprms['args'] = $arguments;
      
$requestprms['debug'] = 0;
      
$requestprms['timeout'] = 0;
      
$requestprms['user'] = NULL;
      
$requestprms['pass'] = NULL;
      
$requestprms['secure'] = 0;

      
$result = xu_rpc_http_concise($requestprms);
       if (
is_array($result) && isset($result['faultCode'])) {
           print(
'Error in xmlrpc call \''.$function.'\''."\n");
           print(
'  code  : '.$result['faultCode']."\n");
           print(
'  message: '.$result['faultString']."\n");
           return
false;
           }
      
$return = $result;
       return
true;
   }

}
overload('xmlrpc_client');

?>
Frank
21-Jan-2004 07:32
here's how to install it on windows (so it actually works):

- php.ini > enable "php_xmlrpc.dll" in extensions.
- php.ini > make sure "extension_dir" is set correctly to find the dll in your php installation dir /extensions.
- copy iconv.dll from your php install dir /dlls to a directory in your path (ex: c:/windows).

if you got some errors while launching apache prior to trying this I suggest you reboot your machine first... sounds weird I know, but remember... you're running Windowz.
Have fun
sjtirtha at gmx dot de
21-Aug-2003 07:23
To install xml-rpc feature on Windows, you need to have "php_xmlrpc.dll" on your "/extensions" Folder.
And you need to enable it on "php.ini".

You need also library from http://xmlrpc-epi.sourceforge.net .
to make your code simply.

Look the examples at http://www.devshed.com/Server_Side/PHP/XMLRPCwithPHP/page1.html .
mistcat attyatatat phreaker dootttt net
18-Apr-2003 12:52
Hope this saves somone some frustration:
As of php 4.3.1 and xmlrpc-epi-php-0.51 php would return a content type text/html instead of text/xml in its responses.  this is a bad thing.  Perl's XMLRPC::Lite for instance will not like you if you do this.  Happily the solution is simple:

header("Content-Type: text/xml");

Happy Hunting.

-Nate
daniel(at)lorch.cc
25-Mar-2003 02:21
If you need a tutorial on the XML-RPC-Extension go to devshed:

  http://www.devshed.com/Server_Side/PHP/XMLRPCwithPHP/page1.html
bmichael at goldparrot dot com
09-Feb-2003 06:52
If anyone is interested in making XMLRPC requests directly from the client, I have been able to get xmlrpc to
work with vcXMLRPC javascript backend.

After about 1 week of scanning the market, I found this solution to be the best on Javascript back end.  It uses the Microsoft.HTTP activeX control for IE, or HTTPRequest Object for Mozilla.

You include vc(Virtual Cowboys) vcXMLRPC.js file into your pages and make the rpc calls from with javascript to create the requests.

It works both ways.

Two Notes:

I have tested it on IE 6.02 and you need to change lines in ProcessRequest :
function to read:

  dom = this.getObject("XMLDOM",http.responseText);

and change the getObject function to use the latest ActiveX Control:

 MSXML2.XMLHTTP.3.0  (or 4.0)
 MSXML2.DOMDocument.3.0  (or 4.0)

The controls are found on MSDN in the Web Services -> XML area.

As another note, you DO NOT NEED the rpcproxy.cgi script to use this.  That is a proxy script to get around JS Security.  You can use PHP to build the proxy.  But, I was able to get the CGI working with GCC compiler on Solaris (change the -KPCI, depend and -x03 optimizer settings in the Makefile )
roland at php dot net
29-Jan-2003 11:15
You can find a good howto about the xml-rpc extension at

http://www.devshed.com/Server_Side/PHP/XMLRPCwithPHP/page1.html

It's an easy client / server example - works quite good :-)
nospam at phppatterns dot com
07-Dec-2002 02:45
Note that you do need the iconv module installed to use the XML-RPC extension (see: http://www.php.net/manual/en/ref.iconv.php)
hfuecks at pinkgoblin dot com
26-Sep-2002 03:34
You can pass PHP errors with the XML-RPC extension as described here: http://www.zend.com/lists/php-dev/200107/msg00642.html
steve at orangeNOSPAMimagineering dot com
24-Aug-2002 10:32
There's a handy library by Keith Devens (version 2.2.1) at
http://www.keithdevens.com/software/xmlrpc/

Here is a sample client. It remotely calls sample.sumAndDifference
with two parameters (3 and 5).
It returns:

sum => 8
difference => -2

<?php
include ("kd_xmlrpc.php");
// define("XMLRPC_DEBUG", 0);    // Set to 1 for handy debugging

$method = "sample.sumAndDifference";
$params = XMLRPC_prepare(array(3,5));

$site = "xmlrpc-c.sourceforge.net";
$location = "/api/sample.php";

list(
$success, $result) = XMLRPC_request( $site, $location, $method, $params );

// XMLRPC_debug_print();    // uncomment for debugging

foreach ( $result as $key => $value ) {
         echo(
" $key => $value \n");
}

?>
hfuecks at pinkgoblin dot com
15-Aug-2002 05:32
This extension does not handle the process of making making XML-RPC client requests via HTTP; it only prepares the XML-RPC request payload.

This differs from many other XML-RPC implementations but offers greater flexibility, allowing SSL connections, authentication headers and XML-RPC via other transports like SMTP.
hfuecks at pinkgoblin dot com
28-Jul-2002 10:33
Anyone interested in PHP-GTK talking to an XML-RPC server:

http://www.pinkgoblin.com/gtk2xmlrpc.php
ivanr at webkreator dot com
21-Jun-2002 08:50
For a really easy way to use this XML-RPC extension take a look at

XML-RPC Class Server (http://www.webkreator.com/php/xcs/)

It automatically creates servers out of PHP classes. Creating clients is almost as easy, especially with the recent addition of the overload extension to PHP (see http://www.php.net/manual/en/ref.overload.php).
nic at uklinux dot NOSPAM dot net
24-Apr-2002 06:05
An alternative XML-RPC implementation is available at http://xmlrpc.usefulinc.com - it's written in PHP so you can use it on servers for which you don't have the luxury of rebuilding PHP on.

nic
cmv at php dot net
08-Jan-2002 02:26
"Latest releases" is a bit redundant, since this extension is bundled into PHP (as of 4.1.0).  You don't need to download anything from sourceforge to make this work.  Just compile PHP with the --with-xmlrpc flag.

The site http://xmlrpc-epi.sourceforge.net/ is useful, however, for documentation.
ravan_n at hotmail dot com
27-Dec-2001 12:01
Refer to the below link for documentation / latest releases of the package.

http://xmlrpc-epi.sourceforge.net/main.php?t=php_about

<xml_set_unparsed_entity_decl_handlerxmlrpc_decode_request>
 Last updated: Sat, 27 Nov 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Tue Nov 30 05:17:37 2004 CET