【问题标题】:PHP: xml-rpc and Turkish charactersPHP:xml-rpc 和土耳其语字符
【发布时间】:2012-03-17 06:43:26
【问题描述】:

我正在尝试通过 xml-rpc 发布到我的 wordpress 网站。

我有两个关于土耳其字符的问题。

1 - 当我在标题中使用其中一个字符 (Çç,Ğğ,Iı,Öö,Şş,Üü) 时,会向 wordpress 发送没有标题的帖子。?

2 - 当我在正文内容中使用相同的字符时,它会以不同的形式发送。

例如,在正文内容中,我有“Ankaralıyım”,而在 wordpress 中则为“Ankaralýyým”。

这是我使用的代码

 <?php
$title="Karaçay";
$body="Ankaralıyım";

$rpcurl="http://localhost/wp/xmlrpc.php";
$username="admin";
$password="pass";
$categories="try";

echo wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$categories,'');

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

    $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)
    );
    $params = array(0,$username,$password,$content,true);
    $request = xmlrpc_encode_request('metaWeblog.newPost',$params, array('encoding'=>'UTF-8'));
    $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, 1);
    $results = curl_exec($ch);
    curl_close($ch);
    return $results;
}

?>

当我从 wordpress 添加帖子时,我没有任何语言问题。

我将如何解决这个问题?

我用不同的代码尝试了同样的事情,这次如果标题或帖子中有任何土耳其字符,我得到了这个结果 创建新的 post-32700 时出错:解析错误。格式不正确 第二个代码是

<?php
        require("class-IXR.php");  
        $client = new IXR_Client('http://localhost/wp/xmlrpc.php');

        $USER = 'admin';
        $PASS = 'pass';

        $content['title'] = 'Test title '.mt_rand();
        $content['categories'] = array("NewCategory","Nothing");
        $content['description'] = '<p>Lorem ırmak ipsum dolor sit amet</p>';
        $content['custom_fields'] = array( array('key' => 'my_custom_fied','value'=>'yes') );
        $content['mt_keywords'] = array('foo','bar');

        if (!$client->query('metaWeblog.newPost','', $USER,$PASS, $content, true))
        {
            die( 'Error while creating a new post' . $client->getErrorCode() ." : ". $client->getErrorMessage());  
        }
        $ID =  $client->getResponse();

        if($ID)
        {
            echo 'Post published with ID:#'.$ID;
        }

?>

这是通过 xml-rpc 将帖子发送到 wordpress 的两种不同方式。但是与英语不同的章节不起作用。我知道xml-rpc支持utf-8。

【问题讨论】:

    标签: xml-rpc


    【解决方案1】:

    您应该将escaping-&gt;markup 参数添加到xmlrpc_encode_request

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

    【讨论】:

    • 这在 8 年后救了我。谢谢!!
    【解决方案2】:

    看来管道的某些部分不理解或不尊重您想要使用 UTF-8 数据。

    您是否尝试过通过tcpdumpnetcatwireshark 等工具检查发送到服务器的原始数据

    您也可以尝试在 curl 中启用调试,使用选项 CURLOPT_VERBOSECURLOPT_HEADER

    curl_setopt($ch,CURLOPT_VERBOSE,1)
    curl_setopt($ch,CURLOPT_HEADER,1)
    

    【讨论】:

    • 尝试将CURLOPT_HEADER 行排除在外,这似乎会在后面的某个步骤中混淆解析。
    • 我已经尝试了正确版本的代码,但我仍然遇到同样的错误。有些字母没问题,但 Iı、Şş、Ğğ 仍然无法正常工作。
    猜你喜欢
    • 2015-01-07
    • 2018-08-03
    • 1970-01-01
    • 2014-07-25
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    相关资源
    最近更新 更多