Parse RSS feeds with PHP

RSS feeds are very common today, and at times we want to write a simple script to grab some information from a feed.

PHP has got and extensive set of functions that can be used to manipulate XML (RSS feeds or even HTML) files. PHP DOM library is one of the handy libraries that can be used to parse RSS feeds in PHP. DOM (Document Object Model) is a standard way for accessing and manipulating XML documents. XML documents can be represented in tree-structure (a node tree), with the elements, attributes, and text defined as nodes.

Below you can find the script for parsing a standard RSS feeds:

// Create a new DOMDocument object
$doc = new DOMDocument();
// Load the RSS file into the object
// Initialize empty array
$arrFeeds = array();
// Get a list of all the elements with the name 'item'
foreach ($doc->getElementsByTagName('item') as $node) {
	$itemRSS = array (
		'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
		'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
		'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
		'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
	array_push($arrFeeds, $itemRSS);
// Output

The getElementsByTagName method is used, within the loop of the item nodes, to get the nodeValue for the title, description, link and date tags. The nodeValue is the text within the node. An array is used to store each set of values and each array represents an entry in the big array that holds our structured RSS data. At the end of the script all the data will be hold by the $arrFeeds array, which is well structured and can be used to display or further manipulation.

One drawback of using the DOM library is that it reads the entire XML document into memory, and then we use the functions for manipulating the data. Thus this method is that is not recommended for large XML documents, which would take too much memory to build the model of the document.

Anyway, usually the feeds we are dealing with are of normal size, and this won’t be an issue at most occasions.

Text to Image in PHP with GD

This is simple script to demonstrate the capabilities of PHP with the GD library, which provides a lot of image functions that can be useful in many applications. GD provides a rich set of functions. For a complete list of these functions, check the PHP manual.

header('Content-Type: image/png');
// Text to be converted to image
$text = 'Hello World';
// Font to use, give accessible path from script
$font = './arial.ttf';
// Convert HTML entities into ISO-8859-1
$text = html_entity_decode($text,ENT_NOQUOTES, "ISO-8859-1");
// Create the image
$im = imagecreatetruecolor(160, 160);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
// Create some colors
imagefilledrectangle($im, 0, 0, 160, 80, $white);
// Add the text
imagettftext($im, 12, 0, 20, 20, $black, $font, $out);

GD can also be used to create and manipulate image files in a variety of different image formats, including GIF, PNG, JPEG, WBMP, and XPM.


  • Though GD supports Unicode text inputs, it doesn’t support complex text rendering
  • Complex Text Rendering is required for properly displaying many language texts, such as the Arabic alphabet and scripts of the Brahmic family, which includes Tamil and many other Indic scripts.

There is a alternative for this using Pango and Cairo in PHP. I’ll post a detailed update on that in my next post.

Activate Oracle on XAMPP for Windows

PHPWith the default installation of XAMPP for Windows, we don’t get PHP Oracle connectivity enabled. This can be enabled easily when you need to connect to a Oracle Database from your PHP application/script. PHP has got the OCI8 extension, which provides Oracle connectivity to PHP application, and OCI8 uses Oracle Instant Client Package to get Oracle specific functions.
Oracle Database
I had the need to connect to a Oracle Database from a PHP script in one of my recent projects, the following is what I did to enable Oracle connectivity in XAMPP for Windows.

  1. In your XAMPP Start Page, go to phpinfo, look for string oci8. If string found it indicate that connection to oracle is available, otherwise to activate connection do the following steps:
  2. Open the currently used php.ini file by looking at the phpinfo, from the XAMPP folder.
  3. Find string ;extension=php_oci8.dll. Remove the semicolon (;) ahead of the string to activate the oracle extension.
  4. Save the php.ini file.
  5. Download the “Instant Client Package – Basic” for Windows from the OTN Instant Client page. Unzip it to c:\instantclient_11_1
  6. Edit the PATH environment setting and add c:\instantclient_11_1 before any other Oracle directories. For example, on Windows XP, follow Start -> Control Panel -> System -> Advanced -> Environment Variables and edit PATH in the System variables list.
  7. Set desired Oracle globalization language environment variables such as NLS_LANG. If nothing is set, a default local environment will be assumed. See An Overview on Globalizing Oracle PHP Applications for more details.
  8. Unset Oracle variables such as ORACLE_HOME and ORACLE_SID, which are unnecessary with Instant Client (if they are set previously).
  9. Restart XAMPP (or Start if its not already started).
  10. To make sure that connection to oracle database has successfully activated, go to phpinfo. Find string: oci8. If found, then XAMPP can now communicate with Oracle Database.

The steps to do the same in a Linux box are almost similar, except there you will use the Linux versions of the packages and setting PATH variables would be different.

You can ping me back with a comment if you run into any issues, I might be able to help you or I can learn from you.