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

问题描述 投票:0回答: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"));}

你有什么想法吗?

php wordpress xml-rpc custom-fields
1个回答
1
投票

好的,这里是:

请勿使用

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

并使用:

xmlrpc_encode_request('blogger.newPost',$params,
                            array('encoding'=>'UTF-8','escaping'=>'markup'));
© www.soinside.com 2019 - 2024. All rights reserved.