Getting an RSS feed with the Zend framework 0.1.1.

March 5th, 2006. Tagged: Zend framework

In case you've missed it, the fisrt pre-release of the Zend PHP framework went public. I briefly looked into it and here's a quick example to get you started.

Download and install

Nothing complicated with the installation, you just get a folder full of PHP goodies and make it available to your scripts.

  1. Get the zip/tar.gz from the framework site
  2. Uncompress somewhere. You'll see 4 folders in the package - demos (some example scripts), documentation, incubator (pre-relase libraries) and library (the real thing)
  3. Copy library somewhere and add it to your include path or just put it in your test scripts directory and have your script set the include path

Personally I simply created a zsandbox in my www and pasted the library in it. Something like:
my web root - C:\apache\htdocs
my test dir - C:\apache\htdocs\zsandbox
the library - C:\apache\htdocs\zsandbox\library

Then in my C:\apache\htdocs\zsandbox\test.php script (accessible as http://localhost/zsandbox/test.php), I started with:

set_include_path(
    get_include_path() . ';library'
)

Don't forget to change ; to : if you're on Linux.

Printing my RSS feed

There is the Zend_Feed package in the library. I played with it to print the contents of this blog's RSS feed. Here's how I did it.

<?php
// set error reporting
error_reporting(E_ALL);
// append 'library' to the current include path
set_include_path(
    get_include_path() . ';library'
);

// get the feed library
require_once 'Zend/Feed.php';

try {
    // get the rss feed
    $rss = Zend_Feed::import('https://www.phpied.com/feed');
} catch (Exception $e) {
    // oops
    echo $e->getMessage();
    exit;
}

// Loop through the items in the feed
foreach ($rss as $item) {
    $items[] = array(
        'title'       => $item->title(),
        'link'        => $item->link(),
    );

}
// var_dump all items
Zend::dump($items);
?>

And the result is:

array(10) {
  [0] => array(2) {
    ["title"] => string(51) "Getting an RSS feed with the  Zend framework 0.1.1."
    ["link"] => string(46) "https://www.phpied.com/rss-feed-zend-framework/"
  }
  [1] => array(2) {
    ["title"] => string(33) "toAjax() in about 6 hours 49 mins"
    ["link"] => string(47) "https://www.phpied.com/to-ajax-in-about-6-hours/"
  }
  [2] => array(2) {
    ["title"] => string(32) "Bitwise operations in JavaScript"
    ["link"] => string(55) "https://www.phpied.com/bitwise-operations-in-javascript/"
  }
  [3] => array(2) {
    ["title"] => string(30) "RGB color parser in JavaScript"
    ["link"] => string(53) "https://www.phpied.com/rgb-color-parser-in-javascript/"
  }
  [4] => array(2) {
    ["title"] => string(9) "coComment"
    ["link"] => string(32) "https://www.phpied.com/cocomment/"
  }
  [5] => array(2) {
    ["title"] => string(27) "Light color fonts in inputs"
    ["link"] => string(50) "https://www.phpied.com/light-color-fonts-in-inputs/"
  }
  [6] => array(2) {
    ["title"] => string(20) "AJAX banner rotation"
    ["link"] => string(43) "https://www.phpied.com/ajax-banner-rotation/"
  }
  [7] => array(2) {
    ["title"] => string(17) "Better time input"
    ["link"] => string(44) "https://www.phpied.com/javascript-time-input/"
  }
  [8] => array(2) {
    ["title"] => string(9) "DB-2-MDB2"
    ["link"] => string(32) "https://www.phpied.com/db-2-mdb2/"
  }
  [9] => array(2) {
    ["title"] => string(38) "Chicken Soup for the Programmer's Soul"
    ["link"] => string(60) "https://www.phpied.com/chicken-soup-for-the-programmers-soul/"
  }
}

Next?

Not bad, eh? So what next? I think the best thing to do it to browse through the manual (scroll down for the examples) and just experiment to get the feel of it.

So far if you ask me, it feels a lot like PEAR, plus MVC on top, but I haven't yet played with the view-controller stuff yet. And it's PHP5 only, which is probably a good thing (hosting companies adoption aside).

p.s. Make sure you read this Andi Gutman's posting to see how to correct a small late-night refactoring issue that slipped into the initial 0.1.1. release of the framework.

Tell your friends about this post on Facebook and Twitter

Sorry, comments disabled and hidden due to excessive spam.

Meanwhile, hit me up on twitter @stoyanstefanov