How to debug AJAX in Drupal using FirePHP

Versions used: 

I've been using the AJAX Framework in Drupal quite a lot lately, and it's always been fairly steady going, but recently my code has become more and more complicated. I thought it was time to find a good way viewing all those PHP arrays and objects during AJAX calls, so I can see how they are changing and if my code was doing what it was supposed to be doing. FirePHP is what I ended up using and below describes how I got it working with Drupal. Note, you need to have the Firebug extension installed, and this post presumes you are using the Firefox browser.

Installing FirePHP for Drupal

  • Install and enable the Devel module.
  • Install and enable the Libraries module.
  • Download the FirePHP library, it's the Standalone version.
  • Extract the FirePHP library and find the folder called FirePHPCore.
  • Copy the FirePHPCore directory into Drupal at the location sites/all/libraries/FirePHPCore. If the libraries directory does not exist, create it.
  • Make sure you have set the correct permissions for the Devel module for whatever user you are using during debugging.

Using FirePHP for Debugging

To output data to the console you need to add the following code:

<?php
$path
= libraries_get_path('FirePHPCore');
if (
file_exists($path . '/fb.php')) {
  include_once
$path . '/fb.php';
  include_once
$path . '/FirePHP.class.php';
 
 
// output your array or object here
 
dfb($array);
}
?>

Open the Console up in Firebug and you should see the values of $array. If you are having problems, be sure that you have Net and Console enabled in Firebug. To output different flavours of messages, you can do so by telling FirePHP what type of message you want logging:

<?php
// output a warning message to console
dfb($variable, 'warning message', FirePHP::WARN);

// output an error message to console
dfb($variable, 'error message', FirePHP::ERROR);
?>

You can use this in your AJAX callback function, for example, to debug and make life easier when using Drupal's AJAX API.