WordPress xmlrpc api远程发布文章php

作者 FarLee 2010年9月16日 0:57:12   ‖浏览(48,961)

不登陆Wordpress博客管理后台可以发表文章吗?答案是肯定的,而且有很多方法可以实现离线发布或远程发布。从wordpress管理后台的撰写选项设置就可以看出,wordpress离线发布可以设置电子邮件服务器登陆用户名密码等信息来发布文章;也可以通过激活 Atom 发布协议开启wordpress Atom Publishing服务进行远程发布;最后还可以通过启用 WordPress api,Movable Type api ,MetaWeblog api和Blogger api的 XML-RPC发布协议(勾选该选项启用),使用桌面客户端软件或远程网站来发布wordpress文章。

WordPress 远程发布桌面客户端

使用xml-rpc发布协议api的wordpress离线发布桌面客户端有许多,比如windows live writer等等。在不同的系统下都有许多wordpress离线发布桌面软件可供使用:

XMLRPC离线发布客户端下载安装后,使用也很简单,输入wordpress 博客的xml-rpc地址,以及登陆用户名密码保存,就可以远程发布写博客文章了。下面主要介绍使用wordpress xml-rpc api实现远程网站发布新文章(metaWeblog.newPost)、获取分类(metaWeblog.getCategories)和最新文章(metaWeblog.getRecentPosts)、新建文章分类(wp.newCategory)、上传图片(metaWeblog.getRecentPosts)等。

XMLRPC API 客户端可以用php、java、c++、perl等多种语言来写,下面远方博客以php代码远程网站发布wordpress文章举例说明api的使用方法,代码贴出并做了详细解释。

xmlrpc api使用php远程发布文章

<?php
require_once(“class-IXR.php”);  // 为WordPress安装包内自带的IXR类库文件:wp-includes/class-IXR.php
$client->debug = true;  //开发测试时设置为true,api完成上线后改为false
$username = “wordpresswriter”; $password = ‘wppass’; $wpsite = ‘http://h.farll.com’; //改成自己的
$xmlclient = $wpsite.”/xmlrpc.php”;
$title = “test title标题”;
$body = ‘测试wordpress xmlrpc api的使用方法,正文包括要上传的图片:<img width=”60″ height=”60″ alt=”” src=”/fckUpload/image/BC537E-8273-4899-853E-F838FC5CD999(2).gif” />,从数据库中获取该文章后自动定时发布”;
$category = array(“Blogger API”,” MetaWeblog API”,”MovableType API”); //这些分类必须存在,否则需先新建
/*metaWeblog API(metaWeblog.getCategories)获取wordpress文章分类*/
$client = new IXR_Client($xmlclient);
$params = array(0,$username,$password);
if (!$client->query(‘metaWeblog.getCategories‘, $params)) {
die(‘Something went wrong – ‘.$client->getErrorCode().’ : ‘.$client->getErrorMessage());
}
$catarray = $client->getResponse();    // 返回的是数组array
foreach ($category as $cat) {
if (in_array($cat,$catarray)==false){ //依次对比,若不存在则新建wordpress分类
/*新建wordpress文章分类*/
$client = new IXR_Client($xmlclient);
$categ = array(
…..
);
$params = array(0,$username,$password,$categ);
if (!$client->query(‘wp.newCategory‘, $params)) {
die(‘Something went wrong – ‘.$client->getErrorCode().’ : ‘.$client->getErrorMessage());
}
}
}
$keywords = array(“keyword1″,”keyword2″,”keyword3”); //和分类不同,若这些tag标签若不存在,则wordpress发布文章的API metaWeblog.newPost会自动创建。
$kjmy_Post_MinSeconds = 60*60*24*3;   //自动定时发布最少时间间隔3天
$kjmy_Post_MaxSeconds = 60*60*24*4;   //自动定时发布wordpress文章时间最多间隔4天

$kjmy_Post_Seconds = mt_rand($kjmy_Post_MinSeconds, $kjmy_Post_MaxSeconds); //远方博客定时发布时间范围3~4天
$client = new IXR_Client($xmlclient);
$params = array(0,$username,$password,1);
if (!$client->query(‘metaWeblog.getRecentPosts‘, $params)) {
die(‘Something went wrong – ‘.$client->getErrorCode().’ : ‘.$client->getErrorMessage()); }
elseif ($myresponse = $client->getResponse()) {
$latesturl = $myresponse[0][‘permaLink’];  echo $latesturl; //返回上一次发表的文章地址
$object = $myresponse[0][‘dateCreated’];   //返回上一次发表文章日期,为IXR_Date对象
$latestpostdate = …..
else {$latestpostdate = strtotime(date(“Y-m-d H:i:s”));}
$posttime = $latestpostdate+$kjmy_Post_Seconds; //发布时间为举例上一篇发表时间3~4天。
/*以下是上传文件图片的API:metaWeblogNewMediaObject—————————————*/

/*在线上传

if (isset($_FILES[‘kjmy_Poster_File’][‘tmp_name’]) && $_FILES[‘kjmy_Poster_File’][‘tmp_name’] != ”){
if ($_FILES[“kjmy_Poster_File”][“error”] > 0){
echo “Error: ” . $_FILES[“kjmy_Poster_File”][“error”] . “<br />”;
} else {
// 请参考metaWeblogNewMediaObject 及 wp_upload_bits函数。

$name = ….
$type = …..
$bits =…..
}
}
*/
/*检测远方博客数据库文章中是否存在图片,有则上传并替换src地址*/
$imgtag = ‘<img’;
$srctag = ‘src’;
$rootpath = $_SERVER[‘DOCUMENT_ROOT’]; //获取网站根目录的服务器路径
// $pattern = ‘/src=\”(.*?)\”/’;
$pattern = “/<img.+src=(‘|\”|)?(.*)(\\1)([\s].*)?>/ismUe”;  //图片代码标签的正则表达式,子模式匹配src地址
if (strpos($body,$imgtag) && strpos($body,$srctag)) {
preg_match_all ($pattern,$body,$matches);
// $newurl = array();
for ($i=0; $i< count($matches[0]); $i++) {
$src = $matches[2][$i];
$imgsrc = $src;
$name = …..
$filesrc = ……
$type = mime_content_type($filesrc); //获取文件类型,php.in需要启用pecl模块
$bits = …….
$data = array(
‘name’=>….
‘type’=>…..
‘bits’=>….
);
// $newurl[$i] = ixrupload($username,$password,$data);
$client = new IXR_Client($xmlclient);
$fileparams = array(0,$username,$password,$data);
if (!$client->query(‘metaWeblog.newMediaObject‘, $fileparams)) {
die(‘Something went wrong – ‘.$client->getErrorCode().’ : ‘.$client->getErrorMessage());
$returnurl=””;
} elseif ($client->getResponse()) {
$response = $client->getResponse();
$returnurl = $response[‘…”];  echo $imgsrc; echo $returnurl;
}
$body = str_replace($imgsrc,$returnurl,$body);  //记得给$body赋值
}
}
/*发表文章*/
$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’,
‘date_created_gmt’=>……..
‘dateCreated’=>$posttime,
‘categories’=>$category,
‘mt_keywords’=>$keywords,
// ‘enclosure’=>$enclosure,这只用于在自定义区域上传音乐等资源,改用metaWeblog.newMediaObject上传
// ‘custom_fields’ =>  array($customfields)
);
// Create the client object
$client = new IXR_Client($xmlclient);
$params = array(0,$username,$password,$content,true); // Last parameter is ‘true’ which means post immideately, to save as draft set it as ‘false’
// Run a query for PHP
if (!$client->query(‘metaWeblog.newPost‘, $params)) {
die(‘Something went wrong – ‘.$client->getErrorCode().’ : ‘.$client->getErrorMessage());
}
else{
echo “<li>文章《”.$title.”》发表成功</li>”;
}

以上就是WordPress xmlrpc api离线远程php网站发布文章的代码,包括上传图片、获取分类等多个实例(注意加省略号的地方请自己补全)。有人要问既然有桌面api,为什么还要用php来开发wordpress 远程发布api呢?

就像上面的例子,我们想想,使用API可以自动定时发布文章(均隔3~4天),Google等搜索引擎喜欢有固定更新频率的wordpress博客,这是seo方面的优势;另外那么我们可以在一天之内将所有文章发表上去,设定每隔多少时间发布,如果文章足够多的话,那么我们甚至可以一年都不用再手动更新和维护wp博客了,而搜索引擎却仍然以固定的频率来抓取定时发表的文章;最后如果我们有N多博客的话,再结合php等代码,对博客群进行更新管理就容易很多了。

WordPress xmlrpc API 列表

WordPress xml-rpc api 接口支持Blogger API, metaWeblog API, Movable Type API 以及wordpress自身的WordPress API。从wordpress 根目录下的xmlrpc.php文件中的wp_xmlrpc_server()函数可以得到所有api的列表:

// WordPress API
'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
'wp.getPage' => 'this:wp_getPage',
'wp.getPages' => 'this:wp_getPages',
'wp.newPage' => 'this:wp_newPage',
'wp.deletePage' => 'this:wp_deletePage',
'wp.editPage' => 'this:wp_editPage',
'wp.getPageList' => 'this:wp_getPageList',
'wp.getAuthors' => 'this:wp_getAuthors',
'wp.getCategories' => 'this:mw_getCategories', // Alias
'wp.getTags' => 'this:wp_getTags',
'wp.newCategory' => 'this:wp_newCategory',
'wp.deleteCategory' => 'this:wp_deleteCategory',
'wp.suggestCategories' => 'this:wp_suggestCategories',
'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
'wp.getCommentCount' => 'this:wp_getCommentCount',
'wp.getPostStatusList' => 'this:wp_getPostStatusList',
'wp.getPageStatusList' => 'this:wp_getPageStatusList',
'wp.getPageTemplates' => 'this:wp_getPageTemplates',
'wp.getOptions' => 'this:wp_getOptions',
'wp.setOptions' => 'this:wp_setOptions',
'wp.getComment' => 'this:wp_getComment',
'wp.getComments' => 'this:wp_getComments',
'wp.deleteComment' => 'this:wp_deleteComment',
'wp.editComment' => 'this:wp_editComment',
'wp.newComment' => 'this:wp_newComment',
'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
// Blogger API
'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
'blogger.getUserInfo' => 'this:blogger_getUserInfo',
'blogger.getPost' => 'this:blogger_getPost',
'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
'blogger.getTemplate' => 'this:blogger_getTemplate',
'blogger.setTemplate' => 'this:blogger_setTemplate',
'blogger.newPost' => 'this:blogger_newPost',
'blogger.editPost' => 'this:blogger_editPost',
'blogger.deletePost' => 'this:blogger_deletePost',
// MetaWeblog API (with MT extensions to structs)
'metaWeblog.newPost' => 'this:mw_newPost',
'metaWeblog.editPost' => 'this:mw_editPost',
'metaWeblog.getPost' => 'this:mw_getPost',
'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
'metaWeblog.getCategories' => 'this:mw_getCategories',
'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
// MetaWeblog API aliases for Blogger API
// see http://www.xmlrpc.com/stories/storyReader$2460
'metaWeblog.deletePost' => 'this:blogger_deletePost',
'metaWeblog.getTemplate' => 'this:blogger_getTemplate',
'metaWeblog.setTemplate' => 'this:blogger_setTemplate',
'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
// MovableType API
'mt.getCategoryList' => 'this:mt_getCategoryList',
'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
'mt.getPostCategories' => 'this:mt_getPostCategories',
'mt.setPostCategories' => 'this:mt_setPostCategories',
'mt.supportedMethods' => 'this:mt_supportedMethods',
'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
'mt.publishPost' => 'this:mt_publishPost',
// PingBack
'pingback.ping' => 'this:pingback_ping',
'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
'demo.sayHello' => 'this:sayHello',
'demo.addTwoNumbers' => 'this:addTwoNumbers'// WordPress API 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs', 'wp.getPage' => 'this:wp_getPage', 'wp.getPages' => 'this:wp_getPages', 'wp.newPage' => 'this:wp_newPage', 'wp.deletePage' => 'this:wp_deletePage', 'wp.editPage' => 'this:wp_editPage', 'wp.getPageList' => 'this:wp_getPageList', 'wp.getAuthors' => 'this:wp_getAuthors', 'wp.getCategories' => 'this:mw_getCategories', // Alias 'wp.getTags' => 'this:wp_getTags', 'wp.newCategory' => 'this:wp_newCategory', 'wp.deleteCategory' => 'this:wp_deleteCategory', 'wp.suggestCategories' => 'this:wp_suggestCategories', 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias 'wp.getCommentCount' => 'this:wp_getCommentCount', 'wp.getPostStatusList' => 'this:wp_getPostStatusList', 'wp.getPageStatusList' => 'this:wp_getPageStatusList', 'wp.getPageTemplates' => 'this:wp_getPageTemplates', 'wp.getOptions' => 'this:wp_getOptions', 'wp.setOptions' => 'this:wp_setOptions', 'wp.getComment' => 'this:wp_getComment', 'wp.getComments' => 'this:wp_getComments', 'wp.deleteComment' => 'this:wp_deleteComment', 'wp.editComment' => 'this:wp_editComment', 'wp.newComment' => 'this:wp_newComment', 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',// Blogger API 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs', 'blogger.getUserInfo' => 'this:blogger_getUserInfo', 'blogger.getPost' => 'this:blogger_getPost', 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts', 'blogger.getTemplate' => 'this:blogger_getTemplate', 'blogger.setTemplate' => 'this:blogger_setTemplate', 'blogger.newPost' => 'this:blogger_newPost', 'blogger.editPost' => 'this:blogger_editPost', 'blogger.deletePost' => 'this:blogger_deletePost',// MetaWeblog API (with MT extensions to structs) 'metaWeblog.newPost' => 'this:mw_newPost', 'metaWeblog.editPost' => 'this:mw_editPost', 'metaWeblog.getPost' => 'this:mw_getPost', 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts', 'metaWeblog.getCategories' => 'this:mw_getCategories', 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',// MetaWeblog API aliases for Blogger API // see http://www.xmlrpc.com/stories/storyReader$2460 'metaWeblog.deletePost' => 'this:blogger_deletePost', 'metaWeblog.getTemplate' => 'this:blogger_getTemplate', 'metaWeblog.setTemplate' => 'this:blogger_setTemplate', 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',// MovableType API 'mt.getCategoryList' => 'this:mt_getCategoryList', 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles', 'mt.getPostCategories' => 'this:mt_getPostCategories', 'mt.setPostCategories' => 'this:mt_setPostCategories', 'mt.supportedMethods' => 'this:mt_supportedMethods', 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters', 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings', 'mt.publishPost' => 'this:mt_publishPost',// PingBack 'pingback.ping' => 'this:pingback_ping', 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks','demo.sayHello' => 'this:sayHello', 'demo.addTwoNumbers' => 'this:addTwoNumbers'
//结束

“WordPress xmlrpc api远程发布文章php”文章评论(20)

  1. 学习!

  2. 不错,正好用到,网页发布文章太慢了,很影响效率的

  3. 夕七彩人间长共中秋八月,杯低吟酒伴同歌盛世高风。

  4. 要好好向前辈学习。我是个菜鸟

  5. 博主写的文章不错,来参观学习。

  6. 过来留个脚印

  7. 今天我尝试用WLW来写博客,可是发现我没办法把我WordPress的账号设置成功,我填了hostname,和wp_path都没用,提示是moved permanently,是不是我的哪里设置错了?求博主帮忙解决下,我上网找了很多资料都没找到。

    • 提示moved permanently,原因一般在于填写hostname的域名的时候,需要注意是否带上了www,如果博客设置了不带www的域名自动跳转到带www的域名,那么填写域名的时候就要带上www,可以看下是不是这个问题。

  8. 能把这个页面的完整代码给我一份吗?

  9. 博主可否发一份代码给我啊?
    邮箱:china.goodname@gmail.com
    拜托了.谢谢博主

Trackbacks/Pingbacks

  1. php获取文件mime类型Fileinfo等方法 – 远方博客
  2. WordPress xmlrpc api远程发布文章php教程 - 暖果博客 | 精彩互联网
  3. WordPress 远程发布桌面客户端 Zoundry Raven « 音韵流逝
  4. WordPress xmlrpc api远程发布文章php - 风行网
  5. Windows Live Writer测试 | 十井博客
  6. 使用WordPress的xml-rpc协议远程发布文章 | ThinkingZen
  7. Bob's Blog » Blog Archive » Blog 接口
  8. 【转载】php获取文件mime类型Fileinfo等方法 | NUI 大本营
  9. WordPress xmlrpc api远程发布文章php - Wynston的博客
  10. XML RPC API详细说明文档(WordPress,CNblogs,CSDN,新浪) | SEO积累|网站分析|网络营销

来说兩句