1

我对 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"));}

你有什么主意吗?

4

1 回答 1

1

好的,这里是:

不要使用

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

并使用:

xmlrpc_encode_request('blogger.newPost',$params,
                            array('encoding'=>'UTF-8','escaping'=>'markup'));
于 2012-09-27T12:11:25.803 回答