当前位置: 首页 > 工具软件 > XMLRPC > 使用案例 >

php wordpress xmlrpc,WordPress xmlrpc api远程发布文章php

禄烨然
2023-12-01

require_once("class-IXR.php");

//

为WordPress安装包内自带的IXR类库文件:wp-includes/class-IXR.php

$client->debug =

true;

//开发测试时设置为true,api完成上线后改为false

$username = "wordpresswriter";

$password = 'wppass'; $wpsite = 'http://zwbeta.com'; //改成自己的

$xmlclient = $wpsite."/xmlrpc.php";

$title = "test

title标题";

$body = '测试wordpress

xmlrpc api的使用方法,正文包括要上传的图片:

width="60″ height="60″

alt=""

src="/fckUpload/image/BC537E-8273-4899-853E-F838FC5CD999(2).gif"

/>,从数据库中获取该文章后自动定时发布";

$category =

array("Blogger

API"," MetaWeblog

API","MovableType

API"); //这些分类必须存在,否则需先新建

$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分类

$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天。

$imgtag = '

$srctag = 'src';

$rootpath =

$_SERVER['DOCUMENT_ROOT']; //获取网站根目录的服务器路径

// $pattern = '/src=\"(.*?)\"/';

$pattern = "//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

"

文章《".$title."》发表成功";

}

?>

 类似资料: