您的位置:首页 > 编程语言 > PHP开发

使用PHP获取Blogger博客RSS或Atom数据

2011-02-12 22:12 369 查看
目前很多的网站提供免费个人博客服务,如Google,新浪,网易等等,如何将免费的博客充分利用起来,需要我们在使用过程中不断总结和思考,对于 程序员来说,如何使用PHP获取Blogger博客RSS或Atom数据显得非常重要,在这里简单的跟大家介绍一下使用PHP获取blogger博客 RSS或Atom数据的基本方法,以PHP获取google的Blogger博客数据为实例,了解PHP获取RSS或Atom数据的基本原理,供参考。

使用前提
有一个Google的Blogger免费空间,如http://shifen.blogspot.com/
获取免费空间的RSS或Atom地址http://shifen.blogspot.com/feeds/posts/default

实例代码

$blogUrl = 'http://shifen.blogspot.com/feeds/posts/default';

$atom = simplexml_load_file ( $blogUrl );

$atom->registerXPathNamespace ( 'atom', 'http://www.w3.org/2005/Atom' );

$title = $atom->title;

$subtitle = $atom->subtitle;

$blogFeeds = $atom->link [0] [href];

$blogURL = $atom->link [2] [href];

$blogNextURL = $atom->link [3] [href];

$entrys = $atom->xpath ( '//atom:entry' );

代码分析
1,定义博客blogger地址,如:$blogUrl = 'http://shifen.blogspot.com/feeds/posts/default';

2,使用PHP内置simplexml_load_file函数将blogger的XML数据转化成对象。

simplexml_load_file相关知识(具体查看PHP手册)
说明:simplexml_load_file 将一个XML文档装载入一个对象中。
原型:simplexml_load_file ( filename [,class_name [,options [, ns [, is_prefix]]]] )

3,使用PHP内置registerXPathNamespace函数为下一次 XPath 查询创建命名空间语境。与前面simplexml_load_file函数组合,支持提供命名空间,Blogger的命名空间使用的是http://www.w3.org/2005/Atom,便于调用Blogger的RSS或Atom数据。

4,获取Blogger的RSS或Atom数据
(1)获取Blogger博客空间标题,如:$atom->title,返回:十分愉快
(2)获取Blogger博客空间次标题,如:$atom->subtitle,返回:学学东西总是好的,能让你十分愉快!
(3)获取Blogger博客RSS地址,如:$atom->link [0] [href],返回:http://shifen.blogspot.com/feeds/posts/default
(4)获取Blogger博客URL地址,如:$atom->link [2] [href],返回:http://shifen.blogspot.com/
(5)获取Blogger博客RSS的下一页地址,如:$atom->link [3] [href],返回:http://shifen.blogspot.com/feeds/posts/default?start-index=26&max-results=25
(6)获取Blogger博客文章内容,如:$atom->xpath ( '//atom:entry' ),返回文章数组,默认最新发布的25篇文章。

上面实例可知,PHP获取Blogger博客RSS或Atom数据使用simplexml_load_fileregisterXPathNamespace两个内置函数即可轻松实现,本文主要是使用PHP获取blogger博客RSS或Atom数据的基本方法,了解其基本原理,下一期将了解如何获取Blogger博客文章内容$atom->xpath ( '//atom:entry' )具体方法。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息