免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2573 | 回复: 0
打印 上一主题 下一主题

原创使用google reader + PHP将iteye博客数据导入到wordpress中 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-16 16:39 |只看该作者 |倒序浏览
原创使用google reader + PHP将iteye博客数据导入到wordpress中






本人wordpress版本为3.0.1,非此版本的,可以查看wp-includes/post.php文件中的wp_insert_post函数进行模仿发布文章,该函数第一个参数即为博客数据数组。

google reader可访问如下地址(红色字体为需要导入的博客链接),建议将xml文件保存至本地。

https://www.google.com/reader/at ... eye.com/rss?n=44444

在wordpress/wp-admin/目录下新建fetch.php文件(然后运行该文件即可),源代码如下:


Php代码
  1. 1.<?php   
  2. 2.require_once ('./admin.php');   
  3. 3.  
  4. 4.$dom = new DOMDocument ( '1.0' );   
  5. 5.//载入保存至本地的xml文件,本人保存至wordpress根目录   
  6. 6.$xmlfile = '../haroldphp.xml';   
  7. 7.  
  8. 8.$dom->load ( $xmlfile );   
  9. 9.  
  10. 10.$dom->saveXML ();   
  11. 11.  
  12. 12.//存储xml数据的数组   
  13. 13.$dataArray = array ();   
  14. 14.  
  15. 15.$entryArray = $dom->getElementsByTagName ( 'entry' );   
  16. 16.  
  17. 17.  
  18. 18.foreach ( $entryArray as $entry ) {   
  19. 19.    //博客标题   
  20. 20.    $dataArray ['post_title'] = $entry->getElementsByTagName ( 'title' )->item ( 0 )->nodeValue;   
  21. 21.      
  22. 22.    //博客发表时间   
  23. 23.    $published = $entry->getElementsByTagName ( 'published' )->item ( 0 )->nodeValue;     
  24. 24.    $dataArray ['post_date_gmt'] = $dataArray ['post_date'] = $published;   
  25. 25.      
  26. 26.    //博客更新时间   
  27. 27.    $updated = $entry->getElementsByTagName ( 'updated' )->item ( 0 )->nodeValue;     
  28. 28.    $dataArray ['post_modified']= $dataArray ['post_modified_gmt'] = $updated;   
  29. 29.      
  30. 30.    $summary = $entry->getElementsByTagName ( 'summary' )->item ( 0 )->nodeValue;   
  31. 31.    //过滤无用数据标签   
  32. 32.    $replace = '/<span style="color:red">\s*<a.*style="color:red">((已有 <strong>\d+<\/strong> 人发表留言,猛击-&gt;&gt;<strong>这里<\/strong>&lt;&lt;-参与讨论)|(本文的讨论也很精彩,浏览讨论&gt;&gt;))/m';   
  33. 33.    $replace2 = '/<span style="color:#e28822">ITeye推荐<\/span>/';   
  34. 34.      
  35. 35.    $replace3 = '~<ul><li><a\s*href="http://(.)*\.iteye\.com/clicks/(\d)+"><span\s*style="color:red;font-weight:bold">.+ </span></a></li></ul>~m';   
  36. 36.      
  37. 37.    $summary = preg_replace ( $replace, '', $summary );   
  38. 38.    $summary = preg_replace ( $replace2, '', $summary );   
  39. 39.    $summary = preg_replace ( $replace3, '', $summary );   
  40. 40.    $dataArray ['post_content'] = $summary;   
  41. 41.    //默认为立即发布,如果需要为草稿,可改为draft   
  42. 42.    $dataArray['post_status'] = 'publish';   
  43. 43.    //使用wp自带发布文章函数将数据写入数据库   
  44. 44.    $post_ID = wp_insert_post ( $dataArray );   
  45. 45.    if (is_wp_error ( $post_ID ) || emptyempty ( $post_ID ))   
  46. 46.        echo $dataArray ['post_title'] . ' --- 导入数据出错';   
  47. 47.           
  48. 48.    add_meta ( $post_ID );   
  49. 49.      
  50. 50.    add_post_meta ( $post_ID, '_edit_last', $GLOBALS ['current_user']->ID   
  51. 51.);     
  52. 52.  
  53. 53.         echo $dataArray ['post_title'] . ' added success!!!';   
  54. 54.}  
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP