如何正確實現PHP擷取部落格資料_PHP教程

來源:互聯網
上載者:User

目前很多的網站提供免費個人部落格服務,如Google,新浪,網易等等,如何將免費的部落格充分利用起來,需要我們在使用過程中不斷總結和思考,對於 程式員來說,如何使用PHP擷取Blogger部落格RSS或Atom資料顯得非常重要,在這裡簡單的跟大家介紹一下使用PHP擷取blogger部落格 RSS或Atom資料的基本方法,以PHP擷取google的Blogger部落格資料為執行個體,瞭解PHP擷取RSS或Atom資料的基本原理,供參考。

PHP擷取部落格資料使用前提

有一個Google的Blogger免費空間。
擷取免費空間的RSS或Atom地址http://shifen.blogspot.com/feeds/posts/default

PHP擷取部落格資料執行個體代碼

 
  1. $blogUrl = 'http://shifen.blogspot.
    com/feeds/posts/default';
  2. $atom = simplexml_load_file ( $blogUrl );
  3. $atom->registerXPathNamespace (
    'atom', 'http://www.w3.org/2005/Atom' );
  4. $title = $atom->title;
  5. $subtitle = $atom->subtitle;
  6. $blogFeeds = $atom->link [0] [href];
  7. $blogURL = $atom->link [2] [href];
  8. $blogNextURL = $atom->link [3] [href];
  9. $entrys = $atom->xpath ( '//atom:entry' );

PHP擷取部落格資料程式碼分析

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擷取部落格資料執行個體可知,PHP擷取Blogger部落格RSS或Atom資料使用simplexml_load_file和registerXPathNamespace兩個內建函數即可輕鬆實現。


http://www.bkjia.com/PHPjc/445915.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445915.htmlTechArticle目前很多的網站提供免費個人部落格服務,如Google,新浪,網易等等,如何將免費的部落格充分利用起來,需要我們在使用過程中不斷總結和思...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.