通过wordpress xmlrpc api发布自定义字段时出现编码问题

时间:2012-09-27 09:53:40

标签: wordpress xml-rpc custom-fields

我在使用wordpress xml-rpc api时遇到了问题。我的代码从xml获取一些数据并发布到博客。页面标题发布得很好,博客上没有问题,但自定义字段已损坏。

代码文件,xml,博客设置和数据库表都是utf-8编码的。

function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$thumbnail,$cfields,$category,$keywords='',$encoding='UTF-8') {

$title = html_entity_decode(htmlentities($title,ENT_NOQUOTES,$encoding));
$body = html_entity_decode(htmlentities($body,ENT_NOQUOTES,$encoding));
$keywords = html_entity_decode(htmlentities($keywords,ENT_NOQUOTES,$encoding));
array_walk($cfields,arr_encoding); // this function does the same thing with above

$content = array(
    'title'=>$title,
    'description'=>$body,
    'mt_allow_comments'=>0,  // 1 to allow comments
    'mt_allow_pings'=>0,  // 1 to allow trackbacks
    'post_type'=>'post',
    'mt_keywords'=>$keywords,
    'categories'=>array($category),
    'custom_fields' => $cfields
);

$params = array(0,$username,$password,$content,true);
$request = xmlrpc_encode_request('metaWeblog.newPost',$params);

$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8" ); 
$results = curl_exec($ch);
if(curl_errno($ch))
    echo '<hr>curl error:'.curl_error($ch)."<hr>";
curl_close($ch);
return $results;}

这是arr encoding函数:

function arr_encoding($cfields){
if(is_array($cfields))
    array_walk($cfields, 'arr_encoding');
else if(is_string($cfields))
    $cfields = html_entity_decode(htmlentities($cfields,ENT_NOQUOTES,"UTF-8"));}

你有什么想法吗?

1 个答案:

答案 0 :(得分:1)

好的,这是:

不要使用

xmlrpc_encode_request('blogger.newPost',$params);

并使用:

xmlrpc_encode_request('blogger.newPost',$params,
                            array('encoding'=>'UTF-8','escaping'=>'markup'));