百科狗-知识改变命运!
--

xmlrpc_encode() - xml-rpc函数

梵高12个月前 (11-21)阅读数 18#技术干货
文章标签函数

xmlrpc_encode()

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

为 PHP 的值生成 XML

说明

xmlrpc_encode(mixed $value): stringWarning

此函数是实验性的。此函数的表象,包括名称及其相关文档都可能在未来的PHP 发布版本中未通知就被修改。使用本函数风险自担。

Warning

xmlrpc_encode() - xml-rpc函数

本函数还未编写文档,仅有参数列表。

Beware this quirk: when you xmlrpc_encode a hash into xmlrpc, numeric keys will not be kept. Even strings containing numbers-only will be lost.
i.e.: xmlrpc_decode(xmlrpc_encode(array('123'=>456)));
returns: array(0=>456);
Apparently this is intentional functionality (see http://bugs.php.net/bug.php?id=21949)
2 workarounds: 
1. prepend/append a string (even whitespace) to all keys, and strip them out at the other end. eg array('key_123'=>456);
2. create an array of hashes. array(array('key'=>123,'value'=>456));
I favour the second option because it's more deliberate.
... this also applies to xmlrpc_encode_request()
This function would be used by an XML-RPC server to convert PHP variables into an XML-RPC response.
It would be used within the PHP functions defined using xmlrpc_server_register_method()
Uses PHP variable reflection to assign the correct data types for the XML-RPC response. For example;

Produces;



 
 
  
  one
  
   red
  
  
  
  two
  
   blue
  
  
  
  three
  
   green
  
  
 
 


While

produces;



 
 
  
  
   red
  
  
   blue
  
  
   green
  
  
 
 


And 

produces



 
 1
 


It's useful to be aware of the settype() function (http://www.php.net/settype).
For binary base64 data and XML-RPC iso8601 date times the xmlrpc_set_type() function should be used on a PHP variable before using xmlrpc_encode()
Worth noting, you can use this function to generate fault response xml.
If you pass a pre-determined array into this function, as follows

... you will get output much like the following:



 
 
  faultCode
  
  4
  
 
 
  faultString
  
  Too many params
  
 
 


All that is needed to do here to make it valid XML-RPC is inject some of your own methodResponse tags.
Check the XML-RPC specification at http://www.xmlrpc.com/spec
Another quirk (more of a bug i think) is the automatic encoding of hashes where all keys that begin with a digit between 1 and 9 are lost:
http://bugs.php.net/bug.php?id=37746
Luckily I found a quick workaround. Just append chr(0x00) to your keys and the xmlrpc response will be correct. Maybe this will apply to encoding of hashes for requests too.
This xmlrpc_encode function doesn't generate the "methodResponse" tag required by some clients.
Using xmlrpc_encode_request seems better:



 
 1
 


xmlrpc_encode_request(NULL,1) generate



 
 
  1
 
 


?>
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)