Friday, July 10, 2009

FB Series: Locally debug your PHP Facebook App

Starting with Facebook applications is a rather a bumpy road. First there is the scattered and often not up-to-date documentation on the Facebook wiki. Its a pain to go through. But ok once you managed to get the important bits its ok.

What I am talking about is the testing. Its quite astonishing that this is not thought through at all: Facebook does not offer a Test environment (!). So this means developers have to test their apps in the real Facebook which means. Developers have to create 2 apps one for test one for prod with different configurations canvas urls etc. as well as create Testusers - or they want to annoy their friends with test apps.

If your like me you prefer debugging to changelogginguploadtestchangeuploadtest cycles. With Facebook Apps this was not straigthforward so therefore here is a easy end-to-end explanation for you how to setup your php facebook app for local debugging - which i didnt find on the net.

Prerequisistes: You have apache and php 5.x installed.

  1. Set up your computer for portforwarding. Portworward.com has good explanations for all different kind of configurations and routers.
  2. Get yourself a alias from dyndns.org, something like myfacebookapp.dyndns.org pointing to your ip. In order to update your dynamic ip if you have one, install the dyndns updater.
  3. Try accessing your apache file via your dyndyns alias(myfacebookapp.dyndns.org). Keep in mind that many routers do not support ip loopback, therefore you might need to go through a proxy to do that. Also make sure to configure your firewall to open the ports your forwarding.
  4. If this all worked you are ready to setup your php environment. Startup (or install) your php ide of choice with integrated xdebugging for php (like Netbeans 6.7 or Aptana 1.5)
  5. You need to configure your php.ini file to support the xdebug. At the end of your php.ini file add:

    zend_extension_ts="c:/php/ext/php_xdebug-2.0.5-5.2.dll"
    xdebug.remote_enable=on
    xdebug.remote_handler=dbgp
    xdebug.remote_mode=req
    xdebug.remote_host=localhost
    xdebug.remote_port=9000

    As you see I use windows change your path to the xdebug .dll / .so as you need. You might need to download it from xdebug.org


  6. Now you should be ready to startup a debug session of a file locally. Test it
  7. In order to debug HTTP Sessions its important that you have the parameter XDEBUG_SESSION_START= (for Netbeans its XDEBUG_SESSION_START=netbeans-xdebug) in your request url. So this gets a bit tricky for facebook apps since facebook is calling the canvas url I think that cannot contain query params (I guess ..) . Anyway no need to try or think either there is a nice firefox plugin that does that for us.

That's it: Fire up a debugging session in your ide, add a breakpoint in your facebook app and try access your facebook app from facebook. You should end up in your local debugging session.

2 comments:

  1. Hey! Your article was very helpful. I did turn to Robert Douglass, http://www.lullabot.com/articles/use-dynamic-dns-host-website-your-laptop, for some further details regarding the dyndns.org and the dyndns updater, but it's all good.

    Yeah, putting in the 'debug' parameter, XDEBUG_SESSION_START=ECLIPSE_DBGP&KEY=124962935150027, doesn't seem to work...I know my Facebook app is running off my local machine though

    I just can't debug it.

    I've used Eclipse extensively and have long configured the php.ini file to support the xdebug.

    Further, I've successfully debugged Facebook Apps using iFrame, but with FBML I've had to get this Dyndns account.

    I don't want to use the firefox plugin you mentioned...I'm in MS explorer right now.

    Would you have a further checklist I can go through and see if I've missed something?

    The current callback URL is just not stopping my breakpoints.

    Canvas Callback URL:
    http://myfacebookapp.dnsdojo.org/joomla_development/Production/index.php?XDEBUG_SESSION_START=ECLIPSE_DBGP&KEY=124962935150027&option=com_facebook&format=fbml&comp=com_facestocks&page=

    ReplyDelete
  2. Hy,

    Sorry about the delay, I'm just moving this blog to my new address: uebersoftware.com - will update the facebook links too.

    To your question: I get the impression that the debuggers for php are generally not so stable in compare to java debuggers. I really had to do some trial and errors and the eclipse setup / with aptana and zend based did not work for me with respect to debugging. I sucessfully use netbeans. Besides stability another advantage to netbeans is that the debugging key for xdebug stays the same.

    ReplyDelete