PHP和JAVA的XML-RPC中文问题解决办法

ZDNet软件频道 时间:2008-07-02 作者: | 中国IT实验室 我要评论()
本文关键词:XML Java PHP Linux
 在使用PHP和JAVA操作XML-RPC的时候,如果request中包含中文字符,会被自动编码成如下样式:
 问题描述:
  
  在使用PHPJava操作XML-RPC的时候,如果request中包含中文字符,会被自动编码成如下样式:
  
  欢欢 。
  
  环境:PHP内置XML-RPC的API,Apache的XML-RPC的Java API
  
  PHP下的解决方法:
  
  起初以为是中文字符的编码问题,所以我就尝试用各种编码方式来编码中文字符,然后交给string XMLrpc_encode_request ( string method, mixed params)函数来生成XML格式的请求,可是依然如故。百思不得其界。便操其Google 一通神搜,也没找到解决办法,后来我找到了http://XMLrpc-epi.sourceforge.net/这个网站。才知道,原来PHP文档里给的XMLrpc_encode_request ( string method, mixed params)函数少了一个可选参数!!!!正确的应该是这样的:string XMLrpc_encode_request(string method, mixed params [, array output_options])!!output_options的结构如下:
  
  $output_options = array(
  "output_type" => "XML",
  "verbosity" => "pretty",
  "escaping" => array("markup", "non-ascii", "non-print"),
  "version" => "XMLrpc",
  "encoding" => "utf-8"
  );
  or
  
  $output_options = array("output_type" => "PHP");
  
  原文说明如下:
  
  output_type: return data as either PHP native data types or XML encoded. ifPHP is used, then the other values are ignored. default = XML
  
  verbosity: determine compactness of generated XML. options are no_white_space, newlines_only, and pretty. default = pretty
  
  escaping: determine how/whether to escape certain characters. 1 or more values are allowed. If multiple, they need to be specified as a sub-array. options are: cdata, non-ascii, non-print, and markup.default = non-ascii, non-print, markup
  
  version: version of XML vocabulary to use. currently, three are supported: XMLrpc, soap 1.1, and simple. The keyword auto is also recognized to mean respond in whichever version the request came in. default = auto (when applicable), XMLrpc
  
  encoding: the encoding that the data is in. Since PHP defaults to iso-8859-1 you will usually want to use that. Change it if you know what you are doing. default=iso-8859-1
  
  经过测试关键是在"escaping" => array("markup")这个值上,将第三个参数传入如下值就可以解决问题了:
  
  $output_options = array(
  "output_type" => "XML",
  "verbosity" => "pretty",
  "escaping" => array("markup"),
  "version" => "XMLrpc",
  "encoding" => "utf-8"
  );

XML

Java

PHP

Linux


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134