問題描述:      在使用PHP和JAVA操作XML-RPC的時候,如果request中包含中文字元,會被自動編碼成如下樣式:

欢欢 。


      PHP下的解決方案:      起初以為是中文字元的編碼問題,所以我就嘗試用各種編碼方式來編碼中文字元,然後交給string xmlrpc_encode_request ( string method, mixed params)函數來產生XML格式的請求,可是依然如故。百思不得其界。便操其Google 一通神搜,也沒找到解決辦法,後來我找到了這個網站。才知道,原來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 = xmlverbosity: determine compactness of generated xml. options are no_white_space, newlines_only, and pretty. default = prettyescaping: 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, markupversion: 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), xmlrpcencoding: 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"                      );

      Apache JAVA XML-RPC的解決方案

     找遍了Apache提供的API好像也沒有找到類似PHP那樣的第三個參數,實在無奈發現他提供了一個Base64的類,我便只好將所有的中文字元在Client端用 Base64編碼,然後request到Server上,然後在Server端在用Base64解碼,哈哈~~~問題也能解決!不過不知道還有沒別的更好的辦法了?

     誰有更好的解決方案請和我聯絡:     QQ:3470431


Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >



如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。