本人wordpress
版本为3.0.1,非此版本的,可以查看wp-includes/post.php文件中的
wp_insert_post函数进行模仿发布文章,该函数第一个参数即为博客数据数组。
google reader可访问如下地址(红色字体为需要导入的博客链接),建议将xml文件保存至本地。
https://www.google.com/reader/atom/feed/
http://haroldphp.iteye.com/rss?n=44444
在wordpress/wp-admin/目录下新建fetch.php文件(然后运行该文件即可),源代码如下:
<?php
require_once ('./admin.php');
$dom = new DOMDocument ( '1.0' );
//载入保存至本地的xml文件,本人保存至wordpress根目录
$xmlfile = '../haroldphp.xml';
$dom->load ( $xmlfile );
$dom->saveXML ();
//存储xml数据的数组
$dataArray = array ();
$entryArray = $dom->getElementsByTagName ( 'entry' );
foreach ( $entryArray as $entry ) {
//博客标题
$dataArray ['post_title'] = $entry->getElementsByTagName ( 'title' )->item ( 0 )->nodeValue;
//博客发表时间
$published = $entry->getElementsByTagName ( 'published' )->item ( 0 )->nodeValue;
$dataArray ['post_date_gmt'] = $dataArray ['post_date'] = $published;
//博客更新时间
$updated = $entry->getElementsByTagName ( 'updated' )->item ( 0 )->nodeValue;
$dataArray ['post_modified']= $dataArray ['post_modified_gmt'] = $updated;
$summary = $entry->getElementsByTagName ( 'summary' )->item ( 0 )->nodeValue;
//过滤无用数据标签
$replace = '/<span style="color:red">\s*<a.*style="color:red">((已有 <strong>\d+<\/strong> 人发表留言,猛击->><strong>这里<\/strong><<-参与讨论)|(本文的讨论也很精彩,浏览讨论>>))/m';
$replace2 = '/<span style="color:#e28822">ITeye推荐<\/span>/';
$replace3 = '~<ul><li><a\s*href="http://(.)*\.iteye\.com/clicks/(\d)+"><span\s*style="color:red;font-weight:bold">.+ </span></a></li></ul>~m';
$summary = preg_replace ( $replace, '', $summary );
$summary = preg_replace ( $replace2, '', $summary );
$summary = preg_replace ( $replace3, '', $summary );
$dataArray ['post_content'] = $summary;
//默认为立即发布,如果需要为草稿,可改为draft
$dataArray['post_status'] = 'publish';
//使用wp自带发布文章函数将数据写入数据库
$post_ID = wp_insert_post ( $dataArray );
if (is_wp_error ( $post_ID ) || empty ( $post_ID ))
echo $dataArray ['post_title'] . ' --- 导入数据出错';
add_meta ( $post_ID );
add_post_meta ( $post_ID, '_edit_last', $GLOBALS ['current_user']->ID
);
echo $dataArray ['post_title'] . ' added success!!!';
}