×

技术坑

zblog远程发布文章API改进实现兼容wordpress xmlrpc

mtsym mtsym 发表于2019-05-04 浏览7689 评论0

最近有个zblog自动发布文章需求,大致看了一下,发现并没有wordpress方便,特别是我还有对接python的要求,大概有几种办法

1. 直接读取/zb_users/c_option.php,暴力写入数据
2. 直接开发一个插件使用zblog内部代码接口
3. 使用zblog本身的xmlrpc

第一种方案为了安全还得自己写鉴权,第二种方案通用性强但是需要对zblog比较了解,第三种方案最方便了,python可以直接使用xmlrpc库对接,由于我已经有wordpress自动发布的python脚本,我想复用这个,因此对zblog的xmlrpc进行改造。

兼容wordpress xmlrpc

找到zb_system/xml-rpc/index.php文件,先在入口判断出增加’mt.supportedMethods’支持,再增加’wp.newPost’支持

case 'mt.supportedMethods':
     $strXML = '<methodResponse><params><param><value><array><data>$%#1#%$</data></array></value></param></params></methodResponse>';
     $strAll = '<value><string>wp.newPost</string></value>';

     echo str_replace("$%#1#%$", $strAll, $strXML);
     break;

case 'wp.newPost':
     $username = (string)$xml->params->param[1]->value->string;
     $password = (string)$xml->params->param[2]->value->string;
     xmlrpc_Verify($username, $password);
     if ($zbp->CheckRights('ArticlePst')) {
         xmlrpc_newPost_as_wp($xml->params->param[3]->value->struct->asXML());
     } else {
         xmlrpc_ShowError(6, __FILE__, __LINE__);
    }
    break;

转化wordpress xmlrpc支持函数如下

```
function xmlrpc_newPost_as_wp($xmlstring)
{
global $zbp;

$xml = simplexml_load_string($xmlstring);

if ($xml) {
    $post = array();
    foreach ($xml->children() as $x) {
        $a = (string)$x->name;
        if ($a == 'terms_names') {
            $struct = $x->value->struct;
            foreach ($struct->children() as $y) {
                $a = (string)$y->name;
                $b = $y->value->children()->asXML();
                $b = str_replace(array('<array>', '</array>', '<data>', '</data>', '<string>', '</string>', '<value>', '</value>', PHP_EOL), array(''), $b);
                $post[$a] = $b;
            }
        } else {
            $b = $x->value->children();
            $b = str_replace(array('<array>', '</array>', '<data>', '</data>', '<string>', '</string>'), array(''), $b);
            $post[$a] = $b;
        }
    }

    $_POST['ID'] = 0;

    $_POST['Title'] = $post['post_title'];

    if (strcmp('publish', $post['post_status']) != 0) {
        $_POST['Status'] = 0;
    } else {
        $_POST['Status'] = 1;
    }

    if (isset($post['mt_basename'])) {
        $_POST['Alias'] = $post['mt_basename'];
    }
    if (isset($post['dateCreated'])) {
        date_default_timezone_set('GMT');
        $_POST['PostTime'] = strtotime($post['dateCreated']);
        date_default_timezone_set($zbp->option['ZC_TIME_ZONE_NAME']);
        $_POST['PostTime'] = date('c', $_POST['PostTime']);
    }
    if (isset($post['wp_author_id'])) {
        $_POST['AuthorID'] = $post['wp_author_id'];
    } else {
        $_POST['AuthorID'] = $zbp->user->ID;
    }
    if (isset($post['post_tag'])) {
        $_POST['Tag'] = $post['post_tag'];
    }
    if (isset($post['category'])) {
        $post['category'] = str_replace('<value>', '', $post['category']);
        $catename = trim(GetValueInArray(explode('</value>', $post['category']), 0));
        $_POST['CateID'] = $zbp->GetCategoryByName($catename)->ID;
    }
    if (isset($post['mt_excerpt'])) {
        $_POST['Intro'] = $post['mt_excerpt'];
    }
    if (isset($post['mt_text_more']) || isset($post['post_content'])) {
        if (isset($post['mt_text_more'])) {
            if ($post['mt_text_more'] != '') {
                $_POST['Content'] = $post['post_content'] . '