Created
January 12, 2013 15:30
-
-
Save JinnLynn/4518491 to your computer and use it in GitHub Desktop.
导出Wordpress日志到新浪博客
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 导出Wordpress日志到新浪博客 | |
* Created by JinnLynn 2013.01.11 http://jeeker.net/ | |
*/ | |
require('./wp-blog-header.php'); | |
require('wp-includes/class-IXR.php'); | |
define('SINA_XMLRPC', 'http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php'); | |
if ($_GET['action'] != 1) { | |
?> | |
<br /> | |
<form action="?action=1" method="post"> | |
新浪账户: <input type="text" name="usr" value="" /><br /> | |
新浪密码: <input type="password" name="pwd" value="" /><br /> | |
<input type="submit" value="导出" /><br /> | |
</form><br /> | |
导出前请确认已开通了新浪博客,并设置了正确的账户密码。 | |
<?php | |
return; | |
} | |
$usr = trim($_POST['usr']); | |
$pwd = trim($_POST['pwd']); | |
if (empty($usr) || empty($pwd)) { | |
echo '<br /><span style="color:red;">用户名或密码错误。</span><a href="javascript:history.back()">back</a>.'; | |
return; | |
} | |
// 登陆测试 | |
$params = array(0, $usr, $pwd); | |
$sina_xmlrpc = new IXR_Client(SINA_XMLRPC); | |
$sina_xmlrpc->query('metaWeblog.getCategories', $params); | |
$res = $sina_xmlrpc->getResponse(); | |
unset($sina_xmlrpc); | |
if (isset($res['faultCode'])) { | |
echo '<br /><span style="color:red;">用户名或密码错误。</span><a href="javascript:history.back()">back</a>.'; | |
return; | |
} | |
$query = new WP_Query(); | |
$query->query( array( 'post_type' => array('post', 'page'), // 导出类型 | |
'order' => 'ASC', | |
'posts_per_page' => 10000 ) | |
); | |
while ($query->have_posts()) { | |
global $post; | |
$query->the_post(); | |
$layout = $post->post_type; | |
$title = get_the_title(); | |
$date = get_post_time('Y-m-d H:m:s'); | |
$author = get_the_author(); | |
$content = $post->post_content; | |
$content = apply_filters('the_content', $content); | |
$content = str_replace(']]>', ']]>', $content); | |
$content .= '<p id="original-post-date" style="color:#ccc"><br />-----<br />原文发表于:'. $date . '</p>'; | |
$sina_post = array( 'title' => $title, | |
'dateCreated' => get_post_time('Y-m-d\TH:m:s\Z', true), //!? 没有作用 | |
'description' => $content); | |
$params = array(0, $usr, $pwd, $sina_post, true); | |
$sina_xmlrpc = new IXR_Client(SINA_XMLRPC); | |
$sina_xmlrpc->query('metaWeblog.newPost', $params); | |
$res = $sina_xmlrpc->getResponse(); | |
unset($sina_xmlrpc); | |
if (isset($res['faultCode'])) { | |
$res = '<span style="color:red;">导出失败</span>'; | |
} | |
echo $title . '<br /> => ' . $res . '<br /><br />'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks very much!