Debug Drupal Services with PhpStorm and Xdebug

It doesn’t matter what kind of Drupal app you’re developing at the moment, but what I’m really sure about is that you can’t live without a full-blown debugger (a.k.a. Xdebug).

The workflow is usually quite simple — set a breakpoint in your IDE (mine is PhpStorm), start a debugging session in your favorite browser using a bookmarklet, URL query, or a custom extension, fire a page and start checking the contents of variables, arrays and objects right away.

Very often though, we’re stuck in a situation when there’s no actual «web browser» available to send a request. The data may come from an external service and handled in Drupal with Services API, RESTful Web Services or just a custom menu callback processing JSON or whatever format you’re working with.

See below a few tricks that I’m using to debug such requests. For sending the data I’ll be using the Postman extension for Chrome.

Appending the parameter to the URL

Postman request

First, you need to figure out what parameter your IDE expects to start a debugging session and then simply append the string to the endpoint URL, like so:

http://example.com/api/send?XDEBUG_SESSION_START=PHPSTORM

In this case, unlike working with a regular browser's bookmarklet, the debugging session will be only active when you have the debugging query string in the address.

Setting a cookie

Another handy method is to send the debugging parameter in a header body and set a cookie.

When using this method, be sure to remove the URL query described above, and set a proper «Cookie» header, as required by your IDE.

What is really cool about this approach is that you don’t have to worry about the URL and be able to quickly enable/disable the cookie by clicking on the blue «checkbox» icon to the left. It makes debugging services a breeze, so try it out.

Happy debugging!