首页 > PHP, WordPress > WordPress通过XML-RPC协议远程发布文章的PHP代码

WordPress通过XML-RPC协议远程发布文章的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://www.brandgoods2009.com&#8217;; //改成自己的
$xmlclient = $wpsite.“/xmlrpc.php”;
$title = “test title标题”;
$body = ‘ 测试wordpress xmlrpc api的使用方法,正文包括要上传的图片:<img width=”60″ height=”60″ alt=”” src=”/fckUpload/image/test.jpg” />,从数据库中获取该文章后自动定时发布”;
$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>”;
}

?>

分类:PHP, WordPress 标签:, , , , ,
  1. 还没有评论。
  1. No trackbacks yet.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: