Quantcast

Get Feeds Data (RSS) using PHP and DOMDocument Class

RSS is very popular syndication format. Many people called it ‘Really Simple Syndication’ or some others called it ‘Rich Site Summary’. Through this article we want to show about how to read RSS feeds using PHP and DOMDocument class. Suppose we have RSS feeds at http://www.sony-ak.com/feed/rss and we want to get all of the recent posts (the article title and link). Here is the code to do that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
  // script name: read_web_feeds.php
  header('Content-Type: text/html; charset=utf-8');
 
  // define the feeds URL
  $xml = "http://www.sony-ak.com/feed/rss";
  $xmlDoc = new DOMDocument();
  $xmlDoc->load($xml);
 
  // get the item tag
  $items = $xmlDoc->getElementsByTagName("item");
 
  // get the items count
  $itemsCount = $items->length;
 
  // start to iterate through the item node
  for ($i=0; $i<$itemsCount; $i++) {
    $itemTitle = $items->item($i)->getElementsByTagName("title")->item(0)->childNodes->item(0)->nodeValue;
    $itemURL = $items->item($i)->getElementsByTagName("link")->item(0)->childNodes->item(0)->nodeValue;
    $item_desc = $items->item($i)->getElementsByTagName("description")->item(0)->childNodes->item(0)->nodeValue;
 
    echo "<a href=\"" . $itemURL . "\">" . $itemTitle . "</a><br/><br/>";
  }
?>

Here is the sample result.

Sample of the feeds result

Sample of the feeds result

Here is the sample of our RSS data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?xml version="1.0" encoding="UTF-8"?><!-- generator="WordPress/2.9.1" -->
<rss version="0.92">
<channel>
	<title>Sony AK Knowledge Center</title>
	<link>http://www.sony-ak.com</link>
	<description>where tacit becomes explicit through codification</description>
	<lastBuildDate>Mon, 15 Feb 2010 15:22:01 +0000</lastBuildDate>
	<docs>http://backend.userland.com/rss092</docs>
 
	<language>en</language>
 
	<item>
		<title>Why your &#8216; (apostrophe) Character Become ‘ and ’?</title>
		<description><![CDATA[During playing to read our WordPress site feeds with PHP, we found some strange behaviour. We have some articles title that contains the apostrophe character (&#8216;). When we read the feed using PHP through our site feed URL, the title that contains the (&#8216;) character become ‘ and ’. For example like below.
Original title:
Import '1_attlog.dat' [...]]]></description>
		<link>http://www.sony-ak.com/2010/02/why-your-apostrophe-character-become-ae%cb%9c-and-ae%e2%84%a2/</link>
			</item>
 
	<item>
		<title>Add Province Options in OrangeHRM for your Country</title>
		<description><![CDATA[Our readers was asking about how to add province data on OrangeHRM that suits with their country. Currently OrangeHRM version 2.5.0.2 only support province data for United States of America (USA). How about provinces in your country? It&#8217;s very easy to customize OrangeHRM to support your country provinces data.
In OrangeHRM, there are two tables:
1. hs_hr_country, [...]]]></description>
		<link>http://www.sony-ak.com/2010/02/add-province-options-in-orangehrm-for-your-country/</link>
			</item>
	<item>
		<title>Checking Batavia Air Flight Schedule using PHP</title>
 
		<description><![CDATA[Here is the PHP code to check Batavia Air flight schedule. Batavia Air is one of Indonesia airlines service. Make sure PHP curl library is installed on your system. We will cover both for one-way and return flight schedule. You can read the sample of usage at the end of the code.
Code for check one-way [...]]]></description>
		<link>http://www.sony-ak.com/2010/02/checking-batavia-air-flight-schedule-using-php/</link>
			</item>
	<item>
		<title>Get Metro TV Headlines News using PHP</title>
		<description><![CDATA[Here is the way to get raw XML based Metro TV news headlines. This data is taken from their web site at http://www.metrotvnews.com.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
&#60;?php
  // check curl library existance
  function_exists&#40;'curl_init'&#41; or die&#40;&#34;curl library is still not installed yet on your machine!&#34;&#41;;
&#160;
  // do access to metro tv
  $curl = curl_init&#40;&#41;;
  curl_setopt&#40;$curl, [...]]]></description>
		<link>http://www.sony-ak.com/2010/02/get-metro-tv-headlines-news-using-php/</link>
 
			</item>
	<item>
		<title>How to Detect curl PHP Library Existance on Your Machine</title>
		<description><![CDATA[Some of our readers often have errors when trying to run a script that contains the curl functions. Here is the error sample.
 
Fatal error: Call to undefined function curl_init() in C:\xampp\htdocs\projects_for_articles\metrotv.php  on line 3
 
It says that &#8216;Call to undefined function curl_init(). The problem is clear because of the curl library is still not enabled [...]]]></description>
		<link>http://www.sony-ak.com/2010/02/how-to-detect-curl-php-library-existance-on-your-machine/</link>
			</item>
	<item>
		<title>How to Show Full Message Headers on Gmail</title>
 
		<description><![CDATA[Sometimes we want to track and read the full message headers on Gmail, and mostly people find difficult to do that. That&#8217;s very easy. You just login to your Gmail account then open the message that you want to read the headers. Click the down arrow next to Reply, at the top-right of the message [...]]]></description>
		<link>http://www.sony-ak.com/2010/02/how-to-show-full-message-headers-on-gmail/</link>
			</item>
	<item>
		<title>Creating Instant favicon.ico from Internet</title>
		<description><![CDATA[If you need to create favicon.ico file you don&#8217;t need special editor for doing that. Now you can do instantly from the internet. There is online service called &#8216;favicon.ico Generator&#8217; located at http://www.favicon.cc. You can draw by yourself and after you finish with the icon then you can download it to your computer.
You can also [...]]]></description>
		<link>http://www.sony-ak.com/2010/02/creating-instant-favicon-ico-from-internet/</link>
 
			</item>
	<item>
		<title>Import Fingerprint Data &#8216;1_attlog.dat&#8217; to OrangeHRM Table &#8216;hs_hr_attendance&#8217; using PHP</title>
		<description><![CDATA[In previous article, Read Fingerprint Attendance Data ‘1_attlog.dat’ Using PHP and Transfer to MySQL, we already talk about importing the fingerprint time attendance data &#8216;1_attlog.dat&#8217; to MySQL table for further process. Now we will try to import the data to OrangeHRM. OrangeHRM is open source Human Resources software that run using PHP + MySQL platform. [...]]]></description>
		<link>http://www.sony-ak.com/2010/02/import-fingerprint-data-1_attlog-dat-to-orangehrm-table-hs_hr_attendance-using-php/</link>
 
			</item>
	<item>
		<title>Backup and Restore MySQL Database using mysqldump</title>
		<description><![CDATA[We will talk about how to do backup and restore on MySQL database using mysqldump utility. Sometimes you need to copy or backup your MySQL database from one machine to another. Fortunately you can do backup easily on MySQL using mysqldump utility.
Let&#8217;s assume you have database called &#8216;your_database_1&#8242; on machine A and you want to [...]]]></description>
		<link>http://www.sony-ak.com/2010/02/backup-and-restore-mysql-database-using-mysqldump/</link>
			</item>
	<item>
		<title>Creating Simple Login System in PHP</title>
 
		<description><![CDATA[We will cover about creating simple login mechanism using PHP and MySQL. Login usually used to seperate the public access page and secured page. Sample of public page is for example like public home page, public profile page, about page, contact page etc. Secured page for example like user profile settings page, user accounts page [...]]]></description>
		<link>http://www.sony-ak.com/2010/02/creating-simple-login-system-in-php/</link>
			</item>
</channel>
</rss>

If you have difficulty please send comment below or drop us e-mail to info@sony-ak.com.

Bookmark and Share

Related posts:

  1. Read Detik.com RSS Feed using PHP and DOMDocument Class
  2. Read The Jakarta Post Headlines RSS Feed using PHP and DOMDocument Class
  3. Create Realtime USGS Earthquake Data Chart with FusionCharts using PHP and Ajax (prototype.js)
  4. Check Node Existance on XML using SimpleXMLElement Class on PHP
  5. Visualize Realtime USGS Earthquake Data using FusionCharts Free, PHP and Ajax (prototype.js)

3 Comments

  1. [...] more: Sony AK Knowledge Center » Get Feeds Data (RSS) using PHP and … [...]

  2. Arif says:

    Mas Sony Kok Error ya :
    Warning: domdocument::domdocument() expects at least 1 parameter, 0 given in C:\xampp\htdocs\tes ajah\rss.php on line 7

    Fatal error: Call to undefined method domdocument::load() in C:\xampp\htdocs\tes ajah\rss.php on line 8

    Tolong infokan ke email saya ya mas.

  3. Sony AK says:

    @arif pake XAMPP versi berapa? Saya pake versi 1.7.3 bisa nih :) Coba upgrade :)

Leave a Reply