小编:alc浏览:29632022-01-17 10:13:58
python中有个库叫python-wordpress-xmlrpc,利用这个可以自动给wordpress网站更新文章。
而zblog的xmlrpc功能和wordpress的基本差不多,所以想要用python写代码实现自动更新也是比较容易的。
但是zblog的xmlrpc文件需要经过简单的改造。
分两个步骤:
第一步:启用XML-RPC协议
登录你的Zblog后台,然后在“网站设置”“全局设置”中启用xml-rpc协议即可。
第二步:对xml-rpc文件进行改造
打开zb_system/xml-rpc/index.php这个文件
在这个文件中找到这个switch语句,大概在943行的样子,添加下面这段代码:

代码如下:
case 'mt.supportedMethods': $strXML = '$%#1#%$ '; $strAll = 'wp.newPost '; 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;
这段代码用来判断请求。接下来,还要对返回的数据格式进行转换。
添加位置大概在第34、35行的样子
看下图吧

也就是第一个if语句结束之后吧,看我上面截图也就明白大概位置了。
代码如下:
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('', ' ', '', '', '', ' ', '', ' ', PHP_EOL), array(''), $b);
$post[$a] = $b;
}
} else {
$b = $x->value->children();
$b = str_replace(array('', ' ', '', '', '', ' '), 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('', '', $post['category']);
$catename = trim(GetValueInArray(explode(' ', $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'] . '' . $post['mt_text_more'];
} else {
$_POST['Content'] = $post['post_content'];
}
} else {
$_POST['Content'] = $post['post_content'];
}
}
$strXML = '$%#1#%$ ';
if (PostArticle() == true) {
$strXML = str_replace("$%#1#%$", 1, $strXML);
echo $strXML;
} else {
xmlrpc_ShowError(0, __FILE__, __LINE__);
}
}
}
这样改造好了之后,就可以使用python编写脚本自动往zblog中发布文章了。接下来分享一个简单的例子。

如果发布成功就会返回一个结果True,否则就会报错。
大家不妨试一下哦。