PHP Tool - Krumo


For all this while, I have been using PHP print_r and var_dump functions to do simple PHP code debugging, until I found this tool called Krumo.

To put it simply, Krumo is a replacement for print_r() and var_dump(). By definition Krumo is a debugging tool (initially for PHP4/PHP5, now for PHP5 only), which displays structured information about any PHP variable.

A lot of developers use print_r() and var_dump() in the means of debugging tools. Although they were intended to present human readble information about a variable, we can all agree that in general they are not. Krumo is an alternative: it does the same job, but it presents the information beautified using CSS and DHTML.

Except the collapsible DHTML tree built around the structure of the dumped PHP variable, and the improved by the CSS looks, Krumo offers additional useful features.

For example haven’t been disappointed that you can not see the name of the variable passed for dumping ? Calling it many times, you will not be able to know which dump value was provided by a certain variable at a certain time in a certain place of your code. Well, Krumo can not do that either, but it offers a work- around: it prints the code line that the Krumo call was placed: in this way you can keep track of the origin of each dump you see on the screen.

Another nice addition to the set of Krumo features is the ability to turn it off. When your code gets swamped with a lot of dumping calls, instead of cleaning them up, you can just turn them off. You go to admit that this beats the alternative of going through your code and removing every dumping route you have … right ?

A lot of times you need to dump the contents of the superglobals ($_GET, $_POST, $_SERVER, $_SESSION, etc), or see the list of included files, or the declared classes and interfaces, or the defined constants, etc. Krumo offers an easy, fast and graceful way to do this with just one simple call: see the examples area for demonstration, or just load this page to see the results.

PHP by example - Simple XML Parsing with SimpleXML


I have read and answered some PHP questions related to XML at DevNetwork Forums, many PHP programmer does not know how to parse XML into string from external resources. Let take an example, I would like to have a Kuala Lumpur weather forecast for my blog, I can retrieve the information from Yahoo! Weather RSS feed in XML formatted document, how do I parse the XML into string using PHP? You just have to use php_get_contents function with SimpleXML extension :

<?php
 
// get kuala lumpur wheather forecast 
$content = file_get_contents('http://xml.weather.yahoo.com/forecastrss?p=MYXX0008');
 
// parse the xml document to array
$xml = new SimpleXMLElement($content);
 
// print the xml element into string
print_r($xml);
 
?>

There is a better way to process the RSS feeds, Read my previous post about Zend_Feed.

Zend Framework - Zend_Feed


Zend_Feed is used to consume RSS and Atom feeds. It is a simple yet powerful module in Zend Framework, to provide programmers a natural syntax for accessing elements of feeds, feed attributes, and entry attributes. Besides that it also has an extensive support to change feed and entry structure, and return new result back into XML, a recommended format to share structured data across different information systems, especially via the internet.

Now I will show you a simple code to access my very own feeds from my blog, since I am using Feedburner, my RSS feeds address will be at http://feeds.feedburner.com/radicalfix:

<?php
 
require_once 'Zend/Feed.php';
 
// a list of feeds you want to import 
$feeds = array ('http://feeds.feedburner.com/radicalfix');
 
foreach($feeds as $feed){
	// import feeds
	$channel = Zend_Feed::import($feed);
 
	$channelTitle =  $channel->title(); // the channel title 
	echo $channelTitle."\n\n"; // print the channel title
 
	/* for this example I will loop through the 
	 * requires RSS channel elements (title, link, decription)
	 * many optional elements are available please refer to 
	 * the latest RRS 2.0 Specification (http://cyber.law.harvard.edu/rss/rss.html) */ 
	foreach($channel->items as $item){
		$itemTitle = $item->title(); // the title of the feed
		$itemDesc = $item->description(); // the description of the feed
		$itemLink = $item->link(); // the link of the feed
 
		echo "\t".$itemTitle."\n\t".$itemDesc."\n\t".$itemLink."\n\n";
	}
}
 
?>

I will show you how to modify the RSS for the next few posts.

References:
RSS 2.0 specification: RSS 2.0