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

xmlrpc_encode_request() - xml-rpc函数

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

xmlrpc_encode_request()

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

为 PHP 的值生成 XML

说明

xmlrpc_encode_request(string $method, mixed $params[,array $output_options]): stringWarning

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

参数

$method

Name of the method to call.

$params

Method parameters compatible with method signature.

$output_options

xmlrpc_encode_request() - xml-rpc函数

Array specifying output options may contain(default values are emphasised):

  • output_type: php,xml

  • verbosity: no_white_space, newlines_only,pretty

  • escaping: cdata,non-ascii, non-print, markup(may be a string with one value or an array with multiple values)

  • version: simple,xmlrpc, soap 1.1, auto

  • encoding:iso-8859-1, other character set supported by iconv

返回值

Returns a string containing the XML representation of the request.

范例

XMLRPC client functions example

参见

  • stream_context_create()创建资源流上下文
  • file_get_contents()将整个文件读入一个字符串
  • xmlrpc_decode()将 XML 译码为 PHP 本身的类型
Binary strings (set with xmlrpc_set_type) go into a ... block like you'd expect. But after every 80th character, this function inserts the XML entity "
", which is a Unicode newline, as if to cause a line-wrap, which is admittedly silly.
Silly though it may be, it causes real problems for some XML-RPC servers, such as http://jakarta.apache.org/xmlrpc/ (nee Helma). Stripping out those entities with something like
$req = preg_replace('/
/', '', xmlrpc_encode_request("my.method", $args));
works around the problem.
It should be noted that encoding does not seem to encode anything, just specify what goes into the XML header.
We had problems with double-encoded UTF strings being saved to database when using this function, sending it of to a apache xml-rpc servlet and storing it in mysql database. It was solved by setting 'escaping' to just 'markup' and 'encoding' to 'UTF-8' (don't forget to set 'utf-8' in xmlrpc_decode too).
It seems that UTF-8 encoded strings gets escaped with their bytes as entities instead of their characters as entites.
The example above is incorrect - the header needs to be an array, see post by "chris dot vigelius at gmx dot net": http://au.php.net/manual/en/function.stream-context-create.php#74431
His post also shows how to do browser authentication, as below:

1 - EDITOR NOTE: THIS IS A FIX FROM "SandersWang dt php at gmail dot com"
This function should be used by an XML-RPC client to create an XML payload for an XML-RPC request;

Produces;


system.methodHelp

 
 
  system.methodSignature
 
 


The second argument recognises the type of variable and generates the correct XML-RPC structure. See xmlrpc_encode() for more details.
Simple OO client with function Overload : 
the php metho test_helloworld is translated to xmlrpc method test.helloworld.
class RpcClient {
  
  private $_methods;
  private $_context;
  private $_url;
  
  function __construct ($url, $user, $passwd) {
    $auth = base64_encode(sprintf('%s:%s', $user,$passwd));
    $this->_context = stream_context_create(array(
      'http' => array(
        'method' => 'POST',
        'header' => "Content-Type: text/xml\r\n".
              "Authorization: Basic $auth" ,
        
      )
    ));
    $this->_url = $url;
    
    $this->registerMethod ("Test_HelloWorld");
    
  }
  
  
  function __call($methodName, $params) {
    if (array_key_exists($methodName,$this->_methods)) {
      // on appelle la fonction RPC
      $m = str_replace('_', '.', $methodName);
      $r = xmlrpc_encode_request($m, $params,array('verbosity'=>'newlines_only'));
      $c = $this->_context;
      stream_context_set_option($c,'http','content',$r);
      $f = file_get_contents($this->_url,false,$c);
      $resp = xmlrpc_decode($f);
      return $resp;
    } else {
      // on appelle la fonction de l'objet
      call_user_method_array($methodName, $this,$params);
    }
  }
  
  private function registerMethod ($method) {
    $this->_methods[$method] = true;
  }
  
}
Note that as far as I can tell, the 
 characters generated by PHP in the base64 fields don't appear to violate the XML-RPC standard at all. XML-RPC messages *are* in XML format, and as such, the XML entities should be getting decoded before being passed to a base64 decoder. So, the previously-mentioned Jakarta-based XML-RPC server appears to violate the XML spec. i.e. There's nothing here that needs to be "fixed" in PHP.
Take care that this function will generate invalid xmlrpc content when invoked with certain parameters (said content will be happily parsed by the lib itself, but not by other implementations).
xmlrpc_encode_request(null, null)
will generate a response without a value
xmlrpc_encode_request('myfunc', array('faultCode' => 666, 'faultString' => 'hello world')
will generated a request containing a  member instead of 
For examples / documentation of the array output_options, see http://xmlrpc-epi.sourceforge.net/main.php?t=php_api#output_options
In short, output_options lets you send compact xmlrpc (without all the "pretty whitespace" xmlrpc_encode adds normally), apply an own escaping table prior to sending, set the encoding, and a couple of other things (the page even says something about soap 1.1 ... I don't know details).
ever tried transmitting an array like the following with xmlrpc?
$var1=array(7=>14,9=>18);
The output array looks quite different! It will look like that:
$var2=array(14,18);
The only solution i found is to prepend a space to the index:
$var3=array(' 7'=>14,' 9'=>18);
Using that method you'll get the right result. ($var1)
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)