Upgrading PEAR
August 22, 2007
To Check What Version of PEAR you have
pear -V
To Upgrade
1) Download (using lynx) and Install (using php) latest Pear
lynx http://go-pear.org | php
If you have problems with lynx
Simple Go to http://go-pear.org and download the file to a local directory (it saves as http://go-pear.org), then run
php http://go-pear.org
Recursion in PHP
June 10, 2007
Recursion is the mechanism in which a function calls itself. This is a powerful feature which can make something complex something simple.
And PHP manual clearly mentions that….
“It is possible to call recursive functions in PHP. However avoid recursive function/method calls with over 100-200 recursion levels as it can smash the stack and cause a termination of the current script”
Recent PHP Security. Org article ( PHP Executor Deep Recursion Stack Overflow) tells you that, all PHP versions are still affected and server crashes and that will kill all other threads of a multithreaded webserver.
The problem is in own words of Zeev “PHP 4.0 (Zend) uses the stack for intensive data, rather than using the heap” and stack fills up pretty quickly in deep recursion.
One way to fix (not the recursive problem but crashing the server) is to use an extension like XDebug to specify the maximum recursion depth thatway php exits with an error but wont crash.
Any more ideas?
How to quickly Parse WordPress RSS Feed using PHP
June 3, 2007
The easy way to parse RSS (Really Simple Syndication) fee from wordpress blog in your personal site using PHP is with PEAR:XML_RSS package.
Installing XML_RSS
Step1 : run Pear install XML_RSS
The chances are you endup with an error saying XML_RSS requires XML_Tree….. so run “pear install –alldeps XML_RSS’ this will download XML_tree….. But later on you might found out that XML_RSS is not working complaining about XML_parser is not found. I blindly assumed that –alldeps will takecare that but it didnt. so you need to do “pear install XML_Parser” also. So in short…..
pear install XML_Parser
pear install XML_Tree
pear install XML_RSS
Step 2: Grab RSS Feed from wordpress and copy to your local disk
There is no point in fetching RSS and parsing when every time user hits or refreshes your webpage as the content is not that dynamic (atleast for me). So I would cache the rss feed and refresh every 24 hrs.
So the php code goes like
<?php
require_once “XML/RSS.php”;
$cache_file = “/tmp/nlakkakula.wordpress.rss”;
if(!file_exists($cache_file) || (filemtime($cache_file) < time() -86400))
{
copy(”http://nlakkakula.wordpress.com/feed“, $cache_file);
}
Step 3: Parsing the Feed
XML_RSS has getItems() method, which grabs all items into an array to make your life easy.
so the rest of the code is
$r = & new XML_RSS($cache_file);
$r->parse();
echo “<h3> Recent Blog Entries From WordPress </h3>”;
echo “<ul>\n”;
foreach ($r->getItems() as $entry)
{
echo “<li><a href=\”".$entry['link'].”\”>”.$entry['title'].”</a></li>\n”;
}
echo “</ul>\n”;
?>
This simple PHP scriptlet grabs WordPress feed and shows the items in your website.