首页 > PHP, WordPress > 用XML-RPC协议读写WordPress上的文章

用XML-RPC协议读写WordPress上的文章

//使用Incutio的XML-RPC库和Blogger API发布文章的例子,其中IXR库也是Wordpress自身所用的库,可以在“wp-includes/class-IXR.php”找到。

<?php
$xmlrpcurl=http://blog.bluesky.cn/xmlrpc.php&#8217;;
$blogid=‘1’;
$username=‘admin’;
$password=‘pass’;
$postTitle=‘标题’;
$postContent=‘正文内容’;

require(‘class-IXR.php’);  // /wordpress/wp-includes/class-IXR.php
$client = new IXR_Client($xmlrpcurl);

$params=array(
,
‘blog_ID’=>$blogid,
‘user_login’=>$username,
‘user_pass’=>$password,
‘post_content’=> . $postContent,
‘publish’=>true
);
$params=array_values($params);

$client->query(“blogger.newPost”,$params);
$response=$client->getResponse();

if ($response[‘faultCode’]==0){
print ‘Success’;
} else {
print ‘Fail:’ . $response[‘faultString’];
}
?>

//以下是用XML-RPC for PHP库和metaWeblog接口发布文章的例子。要先下载phpxmlrpc库取出其中的xmlrpc.inc来用。

<?php
include(“xmlrpc.inc”); // 这里要改成xmlrpc.inc所在的路径
$GLOBALS[‘xmlrpc_internalencoding’] = ‘UTF-8’;

define (‘DOMAIN’, ‘blog.bluesky.cn’); // 博客的域名
define (‘BLOGID’, 1); // 博客ID,一般为1
define (‘USER’, ‘admin’); // 博客登录的用户名
define (‘PASSWORD’, ‘pass’); // 博客登录的密码

// 创建 xml-rpc client
$cl = new xmlrpc_client ( “/xmlrpc.php”, DOMAIN, 80);

// 准备请求
$req = new xmlrpcmsg(‘metaWeblog.newPost’);
// 逐个列出请求的参数:
$req->addParam ( new xmlrpcval ( BLOGID, ‘int’)); // 博客ID
$req->addParam ( new xmlrpcval ( USER, ‘string’ )); // 用户名
$req->addParam ( new xmlrpcval ( PASSWORD, ‘string’ )); // 密码
$struct = new xmlrpcval (
array (
“title” => new xmlrpcval ( ‘标题’, ‘string’ ), // 标题
“description” => new xmlrpcval ( ‘正文内容’, ‘string’), // 内容
), “struct”
);
$req->addParam ( $struct );
$req->addParam ( new xmlrpcval (1, ‘int’)); // 立即发布

// 发送请求
$ans = $cl->send($req);

var_dump ( $ans );
?>

//注意一定要加上:$GLOBALS[‘xmlrpc_internalencoding’] = ‘UTF-8’;这一句,否则不能正确处理中文。

//另一段也是用phpxmlrpc库和metaWeblog接口发表文章的例子代码,该代码使用了返回类型为“phpvals”,这样可以直接用返回结果的$r->val来获取文章ID号。

<?php
include(“xmlrpc.inc”);
$GLOBALS[‘xmlrpc_internalencoding’] = ‘UTF-8’;

$c = new xmlrpc_client(“/xmlrpc.php”, “blog.bluesky.cn”, 80);
$content[‘title’]=“标题”//标题
$content[‘description’]=“正文内容”; //内容
$content[‘mt_keywords’]=“标签1,标签2”//标签
$content[‘wp_password’]=“”//文章密码,输入后显示加密
$content[‘categories’] = array(“分类1”,“分类2”); //分类名
$x = new xmlrpcmsg(“metaWeblog.newPost”,
array(php_xmlrpc_encode(“1”), //BLOG ID
php_xmlrpc_encode(“admin”), //用户名
php_xmlrpc_encode(“pass”), //密码
php_xmlrpc_encode($content),
php_xmlrpc_encode(“1”)));    //立即发表

$c->return_type = ‘phpvals’;
$r =$c->send($x);
if ($r->errno==“0”)
echo “发表成功,文章序号为:”.$r->val;
else {
echo “出错了”;
print_r($r);
}
?>

//以下是读取最新一篇文章的例子代码:

<?php
include(“xmlrpc.inc”);
$GLOBALS[‘xmlrpc_internalencoding’] = ‘UTF-8’;
$c = new xmlrpc_client(“/xmlrpc.php”, “blog.bluesky.cn”, 80);

$msg = new xmlrpcmsg(‘metaWeblog.getRecentPosts’,
array(php_xmlrpc_encode(“1”),
php_xmlrpc_encode(“admin”),
php_xmlrpc_encode(“pass”),
php_xmlrpc_encode(“1”)) );
$res = $c->send($msg);
$val = $res->value();

$val->arraymem(0)->structreset();
while (list($key, $v) = $val->arraymem(0)->structeach())
{
echo “Element: $key; Type: “.$v->kindOf().“; Value:”.$v->scalarval().\n;
}
?>

  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 博主赞过: