找回密码
 开放注册

QQ登录

只需一步,快速开始

微信登录

微信扫码,快速开始

搜索
查看: 684|回复: 0

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

[复制链接]

735

主题

1102

回帖

3076

牛毛

二级牛人

积分
3096
QQ
发表于 2009-7-8 14:53:00 | 显示全部楼层 |阅读模式 来自 山东省威海市
使用PHP获取Blogger博客RSS或Atom数据
目前很多的网站提供免费个人博客服务,如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 ... &max-results=25
(6)获取Blogger博客文章内容,如:$atom->xpath ( '//atom:entry' ),返回文章数组,默认最新发布的25篇文章。
上面实例可知,PHP获取Blogger博客RSS或Atom数据使用simplexml_load_file和registerXPathNamespace两个内置函数即可轻松实现,本文主要是使用PHP获取blogger博客RSS或Atom数据的基本方法,了解其基本原理,下一期将了解如何获取Blogger博客文章内容$atom->xpath ( '//atom:entry' )具体方法。
您需要登录后才可以回帖 登录 | 开放注册

本版积分规则

帮助|Archiver|小黑屋|通信管理局专项备案号:[2008]238号|NB5社区 ( 皖ICP备08004151号;皖公网安备34010402700514号 )

GMT+8, 2025-4-25 11:37 , Processed in 0.136494 second(s), 35 queries .

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表