版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/mayongzhan - 马永占,myz,mayongzhan
metaweblogAPI类
整个项目部分内容,看看意思.其实很简单.关键部分略掉
Xmlrpc_encode不是那么好用,有的东西不能使用.例如struct类型
所以如果有需要的话,还是自己写一个xml结构,然后发过去.OK.
class Send extends SysLOG
{
private $blogid = "xxx"; //博ID
private $username = "xxx"; //用户名
private $userpassword = "xxx"; //用户密码
private $url = " http://blog.csdn.net/mayongzhan/services/metablogapi.aspx"; //地址
function __construct($blogid, $username, $userpassword, $url)
{
$this->blogid = $blogid;
$this->username = $username;
$this->userpassword = $userpassword;
$this->url = $url;
}
//添加类别
public function addCate($cateName)
{
//metaWeblog.newCategory 新增类别
$request = xmlrpc_encode_request(
'metaWeblog.newCategory',
array($this->blogid,
$this->username,
$this->userpassword,
$cateName),
array('encoding' => 'UTF-8')
);
return $this->xmlRPCSend($request); //类别ID
}
//添加文章
public function addPage($title, $content, $down, $cate)
{
略
}
//其他暂时用不到的操作
private function otherOP()
{
//metaWeblog.getCategories 显示类别
//metaWeblog.getPost 得到文章
//metaWeblog.editPost 编辑文章
//blogger.deletePost 删除文章
//blogger.getRecentPosts
//blogger.getUsersBlogs
//metaWeblog.getCategoryPosts
//metaWeblog.getPostByID
//metaWeblog.getRecentPosts
//metaWeblog.newComment
//metaWeblog.newMediaObject
//mt.getCategoryList
//mt.getPostCategories
//mt.setPostCategories
}
private function xmlRPCSend($request)
{
try
{
$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request
)));
$file = file_get_contents($this->url, false, $context);
if(!$file) {
throw new Exception('错误:得不到webservice的response');
}
$response = xmlrpc_decode($file);
if (is_array($response) && xmlrpc_is_fault($response))
{
throw new Exception($response['faultString'],
$response['faultCode']);
}
return $response;
}
catch (Exception $e)
{
$this->sys_log($e->getMessage(), 'errorSend.log');
}
}
}