Search code examples
phpwordpressxml-rpccustom-fields

Encoding issues when posting custom fields via wordpress xmlrpc api


I've a problem with wordpress xml-rpc api. My code gets some data from an xml and posts to a blog. Page title posted well, there is no problem on blog but custom fields are broken.

Code file, xml, blog settings and database tables they are all utf-8 encoded.

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;}

and this is arr encoding function:

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

Do you have any idea?


Solution

  • OK, here it is:

    Don't use

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

    and use:

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