Making FirePHP Accessible to Every PHP Page
I heart FireBug. I loved FireBug so much, I even made a simple Ruby on Rails plugin to log to FireBug, called FireBug Logger.
It was rudimentary, but it did its job. Lucky for me, a similar project exists in the PHP world, appropriately labelled FirePHP.
FirePHP
FirePHP is bit fancier, in that it can format the output that is sent to the FireBug console. But because of this feature, it requires to be installed as a Firefox extension.
This post will not discuss on the benefits of using FirePHP, or how to install it. There are lots more material available out there on that.
Making FirePHP Easily Accessible
Sadly to get FirePHP available to every PHP script, you are required to call the code to load FirePHP (fb.php). But this is not necessarily true.
In your php.ini, there is an option called auto_prepend_file which will automatically include a PHP file before executing the requested PHP file.
So with this can call load FirePHP prior to any execution, and thus making it accessible via any script that is executed.
Lets enable the auto_prepend_file and give it a value to another PHP file.
[block:file]auto_prepend_file = /Users/aizat/src/firephp/firephp.php[/block]
Don't forget to restart your Apache, else your changes won't be applied!
Now inside /Users/aizat/src/firephp/firephp.php, I will require the main FirePHP library like so:
<?php
require_once('lib/FirePHPCore/fb.php');
if (php_sapi_name() != 'cli' && ini_get('output_buffering') == false) {
ob_start();
}
Make sure that the require_once on line 3 points to the location of the fb.php.
require_once('lib/FirePHPCore/fb.php');
Testing
Now lets test it with a very simple file. Create a php file and dump this inside:
<?php
fb("auto_prepend_file successfully worked!", FirePHP::LOG);
Open the file in your web browser, and it should load successfully!
There you have it, easy debugging without manually requiring the FirePHP libraries, and accessible in all PHP files. What more do you want?
Batch Download of Photos From Flickr Based on Their Tag
For the FOSS.my event, I wanted everyone to tag their photos with foss.my or foss.my 2008. Because this makes it easier to easily see FOSS.my buzz as it happens on Flickr!
A Tale of Sucky Wordpress Plugins
I was looking for a plugin to grab Flickr photos by a specified tag, so that we can use it in the foss.my gallery. Though there are many Flickr plugins for Wordpress, there was none catered to my requirements. Unable to find a solution, I thought it was quicker to hack one myself.
So this script just expanded from there, and I quickly had a Flickr batch downloader.
[block:important]There are several requirements:
- Download phpFlickr, and unzip it to the same directory as the script.
- You need to obtain a Flickr API key.
[/block]
Use the source
<?
/*
* phpFlickr
* http://phpflickr.com/
*
* Flickr API
* http://www.flickr.com/services/api/
*
* Flickr API call flickr.photos.search
* http://www.flickr.com/services/api/flickr.photos.search.html
*
* Photos are available via the following format:
* http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}_[mstb].jpg
*
* For more information visit http://www.flickr.com/services/api/misc.urls.html
*
* Applying for a Flickr API Key
* http://www.flickr.com/services/api/keys/apply/
*/
require_once("phpFlickr.php");
define('VERBOSE', array_search('-v', $argv));
// Download a batch of photos from the given page
function download($photos) {
$download = 0;
foreach ($photos['photo'] as $photo) {
$filename = sprintf("pics/%s-%s.jpg", $photo['owner'], $photo['id']);
$url = sprintf("http://farm%d.static.flickr.com/%s/%s_%s_o.jpg", $photo['farm'], $photo['server'], $photo['id'], $photo['secret']);
if (file_exists($filename)) {
if (VERBOSE)
print sprintf("33[33m %-10s %s33[0m \n", "Skip", $url);
else
print sprintf("33[33m.33[0m");
continue;
}
if (VERBOSE)
print sprintf("33[32;1m %-10s %s33[0m \n", "Download", $url);
else
print sprintf("33[32m.33[0m");
file_put_contents($filename, file_get_contents($url));
$download += 1;
}
return $download;
}
$flickr = new phpFlickr("YOUR_API_KEY");
$options = array('tags' => 'fossmy, "fossmy 2008", foss.my, "foss.my 2008"');
$photos = $flickr->photos_search($options);
print sprintf("Available 33[32;1m %d 33[0m \n", $photos['total']);
// Download the first batch of photos
$downloaded = download($photos);
$page = $photos['page'];
$pages = $photos['pages'];
// Download the following batch of photos
for ($i = 2; $i < $pages; $i++) {
$options['page'] = $i;
$photos = $flickr->photos_search($options);
$downloaded += download($photos);
}
if (! VERBOSE)
echo "\n";
print sprintf("Downloaded 33[32;1m %d 33[0m/ %d \n", $downloaded, $photos['total']);
Running it
It's just as a simple as:
[block:terminal]php download.php[/block]
Sample Results:
[block:terminal]
Available 492
..................[trim]............
Downloaded 0 / 492
[/block]
It even comes with a -v flag, so you can view what files are being downloaded.
Very Important Programmer (VIP) PHP Programming Competitiong
Last weekend, Sunday August 24th 2008, I participated in the Very Important Programmer (VIP) PHP Programming Contest, sponsored by the people of e-Melaka, Bahagian K-Ekonomi
JKM Melaka, aist, Zend, Jaticom and training.org.my.
Lots of familiar faces from the local PHP scene.
Alwin Chan, and Raj Kissu and myself teamed up as team "Paamayim Nekudotayim", which I had no idea how to spell (and pronounce) throughout the competition.
Though the event had some hiccups, I believe it ran quite well. Having been to other programming competitions, I believe this one had to be one of the most accommodating. Food and drinks were readily available throughout the competition, though they might not be suitable for you vegesaurs out there. The selection of food, went from proper meals to snacks, and the drinks were nicely chilled in a fridge, which was nicely positioned near us.
Support the organizers! I believe they did a great job!
Congrats goes out to Alwin Chan and Raj Kissu as well.
Team Paamayim Nekudotayim FTW!


