---
title: "原创使用google reader + PHP将iteye博客数据导入到wordpress中"
author: "再逢山水"
date: "2011-11-15T18:14:24Z"
tags: []
categories: ["编程语言"]
slug: "google-reader-php-iteye-wordpress"
draft: false
lastmod: "2011-11-15T18:19:23Z"
id: 1260607
url: "https://haroldphp.iteye.com/blog/1260607"
---

本人wordpress版本为3.0.1，非此版本的，可以查看wp-includes/post.php文件中的<span style="color:blue">wp_insert_post</span>函数进行模仿发布文章，该函数第一个参数即为博客数据数组。

google reader可访问如下地址（红色字体为需要导入的博客链接），建议将xml文件保存至本地。

https://www.google.com/reader/atom/feed/<span style="color:red">http://haroldphp.iteye.com/rss</span>?n=44444

在wordpress/wp-admin/目录下新建fetch.php文件（然后运行该文件即可），源代码如下：


```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> 人发表留言，猛击-&gt;&gt;<strong>这里<\/strong>&lt;&lt;-参与讨论)|(本文的讨论也很精彩，浏览讨论&gt;&gt;))/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!!!';
}



```