RSS

Four Ways To Ease Facebook Application Development

0 Comments | This entry was posted on Mar 24 2010

For the last two months I have been developing a couple of Facebook applications for clients. Developing new apps for Facebook can be difficult and very time consuming in the fact that the applications need to be hosted on a publicly available server rather than in your standard dev environment. This is a pain for serveral reasons including the need to upload files each time a change is made and that you don’t want php or debugging messages being displayed.

Therefore you will want to send the debug and error messages elsewhere that you can easily watch. These tips are not complicated and I would hope that they are used by most developers at least some of the time for all projects.

1. Custom logger

Rather than printing debug messages to the screen I suggest that you send them to a custom log file which you can watch as new entries are added. Create a function similar to the following:

function logger( $msg )
{
   file_put_contents( 'log.txt', date( 'Y-m-d H:i:s' ) ." $msg\n", FILE_APPEND );
}

Once this function has been defined, you can easily send debugging messages to the log like:

logger( 'name is set: '. $name );

The new string and the set variable will be appended to the end of the log.

2. PHP error logs

When working in your own dev environment it is a must that you have errors sent directly into the browser. This way any warnings or fatal errors are immediately shown to you and you can fix and move on. This is undesirable (for several reasons) for a publicly available site so you need to log these to a file which you should also watch.

There are several methods to set PHP logging:

Enabling PHP error logging through Apache config

This in itself can be added in two places. The first option should only be available if you have root privileges to the server. Find the Apache virtualhost record (apache2ctl -S is handy for this) and set add the following:

php_value error_reporting 6143
php_flag log_errors on
php_value error_log /var/log/apache2/vhosts/yourdomain-php_error.log

The second option is to create a file named .htaccess in the web root directory and add the same options. This may require AllowOverride to be set to All in the virtual host record for this to work.

Enabling PHP error logging with PHP

The same options can be added directly into your scripts with like this:

ini_set( 'error_reporting', 6143 );
ini_set( 'log_errors', 'on' );
ini_set( 'error_log', '/var/log/apache2/vhosts/yourdomain-php_error.log' );

If you plan to develop for a long period it would be best to set the log file to go into the /tmp directory so it doesn’t cause hard disk issues on your server.

3. Apache logs

Apache logs are also very useful in developing Facebook applications. By watching these files you can see when and what Facebook is downloading from your server. I found this extremely useful when making Ajax calls to the server to see what $_GET variables were being sent.

These log files can usually be found somewhere in /var/log/apache2 but it may be easier again to check with apache2ctl -S to see exactly where the log is being saved.

Watching the logs

The best way to watch these log files is to SSH into the server and follow the logs with the tail command with it’s -f follow parameter.

tail -f /var/log/apache2/vhosts/yourdomain-php_error.log

By following the file you don’t need to keep closing and re-opening the file to see new entries.

4. Rsync

The best tool to upload any files that hae been changed is to use the rysnc command. This tool will compare your local files with the remote ones and upload any changes found. This beats the hell out of using FTP. I usually create a script which I run with looks like this:

rsync -r --verbose ./public_html/* username@hostname:/var/www/yourdomain.com/public_html

This will continually prompt for a password but this can be overcome by setting ssh keys. Follow this tutorial on how to set-up ssh keys.

Conclusion

I hope this helps others develop and debug Facebook applications. If you have further hints or ideas, I would love to hear them.