Archive for November, 2009

PHP RSS/Atom Reader Library menggunakan Zend Feed Reader

Kalau kamu sedang atau akan membuat aplikasi web yang dapat mengambil dan membaca feed seperti RSS atau Atom dari suatu situs, Zend Feed Reader bawaan Zend Framework bisa kamu jadikan pilihan. Versi yang saya gunakan di sini Zend Framework 1.9.5. Bila kamu hanya perlu library feed readernya saja, berikut library Zend Feed Reader yang sudah saya pisahkan dari Zend Framework. download di sini.

API yang disediakan oleh library ini sangat memudahkan kita untuk mendapatkan data atau content yang ada di feed. Versi feed yang didukung yaitu RDF/RSS 1.0, RSS 2.0 and Atom 0.3/1.0.
Cukup dengan memberikan URI feed tersebut berada, library yang mengurus sisanya. Berikut cuplikan kodenya:

$feed = Zend_Feed_Reader::import('http://www.syabac.web.id/feed/rss');
$data = array(
    'title'        => $feed->getTitle(),
    'link'         => $feed->getLink(),
    'dateModified' => $feed->getDateModified(),
    'description'  => $feed->getDescription(),
    'language'     => $feed->getLanguage(),
    'entries'      => array(),
);
 
foreach ($feed as $entry) {
    $edata = array(
        'title'        => $entry->getTitle(),
        'description'  => $entry->getDescription(),
        'dateModified' => $entry->getDateModified(),
        'author'       => $entry->getAuthor(),
        'link'         => $entry->getLink(),
        'content'      => $entry->getContent()
    );
    $data['entries'][] = $edata;
}

Selain mengimport dari URI, kita juga bisa mengimport dari string XML atau local file. Berikut kodenya:

// from a URI
$feed = Zend_Feed_Reader::import('http://www.syabac.web.id/feed/');
 
// from a String
$feed = Zend_Feed_Reader::importString($feedXmlString);
 
// from a file
$feed = Zend_Feed_Reader::importFile('./feed.xml');

Selain untuk membaca data dari feed, kita juga bisa memanipulasi dan menyimpan feed yang sudah kita import tadi.

Finding and Locating Feed URIs from website

Selain fitur-fitur yang sudah saya tulis di atas, Zend Feed Reader juga menyediakan API yang berfungsi mencari atau mendapatkan feed URI suatu situs. Berikut contohnya:

$links = Zend_Feed_Reader::findFeedLinks('http://www.syabac.web.id');
if(isset($links->rdf)) {
    echo $links->rdf, "\n"; // http://www.syabac.web.id/feed/rdf/
}
if(isset($links->rss)) {
    echo $links->rss, "\n"; // http://www.syabac.web.id/feed/rss/
}
if(isset($links->atom)) {
    echo $links->atom, "\n"; // http://www.syabac.web.id/feed/atom/
}
//mencetak semua feed URI yang ditemukan
foreach ($links as $link) {
    echo $link['href'], "\n";
}

Berikut beberapa API yang disediakan Zend Feed Reader (diambil dari Zend Framework Manual).

Feed Level API Methods

getId() Returns a unique ID associated with this feed
getTitle() Returns the title of the feed
getDescription() Returns the text description of the feed
getLink() Returns a URI to the HTML website
containing the same or
similar information as this feed (i.e. if the feed is from a blog,
it should provide the blog’s URI where the
HTML version of the entries can be read).
getFeedLink() Returns the URI of this feed, which should be the
same as the URI used to import the feed
getAuthors() Returns an array of all authors associated with this feed
including email address in the author string if available
getAuthor(integer $index = 0) Returns either the first author known, or with the
optional $index parameter any specific
index on the array of Authors (returning null if an
invalid index).
getDateCreated() Returns the date on which this feed was created. Generally
only applicable to Atom where it represents the date the resource
described by an Atom 1.0 document was created.
getDateModified() Returns the date on which this feed was last modified
getLanguage() Returns the language of the feed (if defined) or simply the
language noted in the XML document
getGenerator() Returns the generator of the feed, e.g. the software which
generated it. This may differ between RSS and Atom
since Atom defines a different notation.
getCopyright() Returns any copyright notice associated with the feed
getHubs() Returns an array of all Hub Server URI endpoints which
are advertised by the feed for using with the Pubsubhubbub
Protocol, allowing subscriptions to the feed for real-time updates.
getDomDocument() Returns the parent
DOMDocument object for the
entire source XML document
getElement() Returns the current feed level
DOMElement object
saveXml() Returns a string containing an XML document of the
entire feed element (this is not the original
document but a rebuilt version)
getXpath() Returns the DOMXPath object
used internally to run queries on the
DOMDocument object (this
includes core and Extension namespaces
pre-registered)
getXpathPrefix() Returns the valid DOM path prefix prepended
to all XPath queries matching the feed being queried
getEncoding() Returns the encoding of the source XML document
(note: this cannot account for errors such as the
server sending documents in a different encoding)
count() Returns a count of the entries or items this feed contains
(implements SPL Countable
interface)
current() Returns either the current entry (using the current index
from key())
key() Returns the current entry index
next() Increments the entry index value by one
rewind() Resets the entry index to 0
valid() Checks that the current entry index is valid, i.e.
it does fall below 0 and does not exceed the number
of entries existing.
getExtensions() Returns an array of all Extension objects loaded for
the current feed (note: both feed-level and entry-level Extensions
exist, and only feed-level Extensions are returned here).
The array keys are of the form {ExtensionName}_Feed.
getExtension(string $name) Returns an Extension object for the feed registered under the
provided name. This allows more fine-grained access to
Extensions which may otherwise be hidden within the implementation
of the standard API methods.
getType() Returns a static class constant (e.g.
Zend_Feed_Reader::TYPE_ATOM_03,
i.e. Atom 0.3) indicating exactly what kind of feed
is being consumed.

Entry Level API Methods

getId() Returns a unique ID for the current entry
getTitle() Returns the title of the current entry
getDescription() Returns a description of the current entry
getLink() Returns a URI to the HTML version
of the current entry
getPermaLink() Returns the permanent link to the current entry
getAuthors() Returns an array of all authors associated with this entry
including email address in the author string if available
getAuthor($index = 0) Returns either the first author known, or with the
optional $index parameter any specific
index on the array of Authors (returning null if an
invalid index).
getDateCreated() Returns the date on which the current entry was
created. Generally only applicable to Atom where it
represents the date the resource described by an
Atom 1.0 document was created.
getDateModified() Returns the date on which the current entry was last
modified
getContent() Returns the content of the current entry (this has any
entities reversed if possible assuming the content type is
HTML). The description is returned if a
separate content element does not exist.
getEnclosure() Returns an array containing the value of all
attributes from a multi-media element including
as array keys: url,
length, type.
getCommentCount() Returns the number of comments made on this entry at the
time the feed was last generated
getCommentLink() Returns a URI pointing to the HTML
page where comments can be made on this entry
getCommentFeedLink(string $type =
'atom'|'rss')
Returns a URI pointing to a feed of the provided type
containing all comments for this entry (type defaults to
Atom/RSS depending on current feed type).

Informasi lebih lanjut dan lengkap ada di Zend Framework Manual.

Referensi: Zend Framework 1.9.5 Manual

Installing PHP 5.x and Apache 2.2.x as CGI in Windows

Baik, kali ini saya akan coba sharing bagaimana konfigurasi Apache web server agar mendukung dan menjalankan PHP. Mungkin kamu sudah pernah atau sering menggunakan paket AMP (Apache, MySQL, dan PHP) seperti XAMPP, WAMP, LAMPP, PHPTriad, AppServ dll. Kalau kamu penasaran gimana konfigurasi Apache supaya dapat menjalankan skrip PHP, berikut saya share caranya.

Ada dua(?) macam cara untuk membuat Apache support PHP, yaitu menjadikan PHP sebagai module Apache (biasanya juga disertakan module Apache di dalam paket distribusi PHP) dan PHP sebagai CGI/Fast-CGI. Yang kali ini saya tulis konfigurasi PHP sebagai CGI application.
Caranya cukup mudah, yang perlu disiapkan adalah installer Apache, dan paket binary PHP terbaru (biasanya dalam bentuk arsip .zip).

Instal Apache terlebih dahulu. Next-click saja, kecuali kalau mau custom installation. Asumsi folder instalasi Apache ada di “C:\Program Files\Apache Software Foundation\Apache2.2″ selanjutnya saya sebut APACHE_ROOT saja.

Langkah berikutnya, ekstrak arsip paket PHP di folder tertentu, misal di C:\php-5.3.0
Kemudian edit file APACHE_ROOT\conf\httpd.conf menggunakan text editor, misal Notepad atau Notepad++. Lalu tambahkan pada akhir file tersebut:

<FilesMatch "\.php$">
   SetHandler application/x-httpd-php-cgi
</FilesMatch>
<IfModule actions_module>
   Action application/x-httpd-php-cgi "/php/php-cgi.exe"
</IfModule>
<IfModule mime_module>
    AddType text/html .php .phps
</IfModule>
ScriptAlias /php/ "C:/php-5.3.0/"
<Directory "C:/php-5.3.0">
   AllowOverride None
   Options None
   Order deny,allow
   Deny from all
<Files "php-cgi.exe">
Allow from all
</Files>
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html index.php index.phtml index.php4 index.htm default.html default.php
</IfModule>

Simpan file tersebut. Restart Apache kalau sudah running. Kemudian coba apakah skrip PHP sudah bisa dieksekusi.
Misal kita buat file phpinfo.php di dalam document root dari Apache, biasanya ada di APACHE_ROOT\htdocs.
Ketikan saja skrip berikut:

phpinfo();

Lihat di web browser apakah muncul seperti gambar berikut. Kalau muncul maka kamu telah berhasil.

FAQ:
Q: File httpd.conf tidak bisa di simpan di folder APACHE_ROOT\conf?
A: Mungkin ada masalah di hak akses atau permission untuk menuliskan/mengubah isi folder tersebut. Coba jadikan file tersebut writable dengan mengubah security levelnya di properties-nya.

Q: Apache versi berapa yang digunakan?
A: Pada contoh di atas, saya menggunakan Apache versi 2.2.11. Jadi kamu bisa menggunakan versi yang sama atau lebih baru dengan yang saya gunakan.

Q: PHP versi berapa aja yang didukung?
A: Untuk konfigurasi di atas tersebut diperuntukan untuk PHP versi 5+

Q: Apakah support di Windows Vista atau di Windows 7?
A: Konfigurasi yang saya lakukan di atas itu dilakukan menggunakan Windows 7 Ultimate, seharusnya juga bisa di Windows Vista. Saya juga pernah mencobanya di Windows Server 2008 dan dapat berjalan dengan baik. Windows XP? Saya rasa tidak akan ada masalah untuk Windows XP.

Q: Koq Apache saya ga mau running, padahal udah disetting automatic di Service Console?
A: Coba kamu jalanin Apache menggunakan console atau command prompt. Biasanya ada pesan error yang muncul kalau ada kesalahan.