Debug PHP application which originates from AJAX request

I have a PHP application in PHPStorm which runs on my test server (http://localhost/apps/php/my-php-app). So when I debug it as a PHP web application, all is fine: I run the debugger, it opens Chrome with appropriate address and the app stops at breakpoint inside its PHP code.

The problem is that this PHP application is designed to serve json responses to AJAX requests coming from javascript apps. I would like to debug some aspects of it which only make sense if the request is real, i.e. it's coming from AJAX request and not directly from browser. But I don't know how to start the debugger from AJAX, there is only option to start it from an address in browser.

I tried to change the PHP Web Application debug config url to the url of the AJAX app (http://localhost/apps/javascript/my-javascript-app) and then run the debugger. The debugger opens the javascript app in the browser, which sends the AJAX request and my PHP app sends the response, but it doesn't stop at breakpoint.

Is it possible to configure the debugger for such scenario?

5 comments

Hi,

You also might need to have a separate debugging sessions for PHP and for JavaScript (AJAX part) at the same time. Check this guide to see how to configure this: <https://confluence.jetbrains.com/display/PhpStorm/Debugging+PHP+and+JavaScript+code+at+the+same+time+in+PhpStorm>.

0

I think the above configuration assumes that both php and javascript apps are under the same Project root. It's not the case for my projects.

PHP app is in I:\xampp\htdocs\apps\laravel\ (Windows path for PHPStorm project root) and is available via url http://localhost/apps/laravel/some-api-path. This app doesn't have any Javascript code, it only accepts and returns Json.

Javascript app is in I:\xampp\htdocs\apps\javascript (Windows path for PHPStorm project root) and is available via url http://localhost/apps/javascript/some-path. This app doesn't have any PHP code.

Following the tutorial above, I managed to "listen to PHP Debug connections" and initiate the PHP app directly from the browser. I didn't manage to configure Javascript debugging, but I don't need to debug Javascript using PHPStorm (Chrome developer tools can be used for that), I only need to debug PHP which runs in response to an external (Javascript AJAX) request.

So, the scenario I need to set up is this, step by step:

  1. Navigate to http://localhost/apps/javascript/some-path in the browser
  2. Click a button. Under the hood, the button sends an AJAX request to url http://localhost/apps/laravel/some-api-path
  3. Make the PHP laravel app (which is open in PHPStorm) stop while processing this request.

Is it possible to achieve? If not, why not? Is the fact that those apps are in separate PHPStorm projects a problem? (another approach I am thinking of is to join the two apps temporarily in one PHPStorm project, just for the sake of debugging. That would require some configuration work, and I am not sure if it would solve the problem)

1

Hi there,

Try installing any xdebug-oriented extension for your browser -- https://confluence.jetbrains.com/display/PhpStorm/Browser+Debugging+Extensions

Using such extension, when "debug" mode is activated, it will set special xdebug cookie which should be sent with subsequent requests (AJAX request in your case). Once PHP starts processing your page, xdebug will check if such cookie (or GET/POST parameter) is present in request .. and if present-- will initiate debug session.

This also requires that PhpStorm is already listening for incoming xdebug requests (the "phone handle" button) -- https://confluence.jetbrains.com/display/PhpStorm/Zero-configuration+Web+Application+Debugging+with+Xdebug+and+PhpStorm

Possible alternatives to browser extension:

  • bookmarklets -- http://www.jetbrains.com/phpstorm/marklets/
  • use "xdebug_break();" in your PHP code
  • use "xdebug.remote_autostart = 1" in your -- in this case xdebug will attempt to debug every single request (which may not be a desired behaviour in some cases, especially if you have lots of automatic (timer based) AJAX requests etc

You may also may need to increase max number of simultaneous debug connections (by default's it's just 1) in PhpStorm settings (that's if you are doing sub-requests .. or more than one request/debug session at a time) -- "Settings/Preferences | Languages & Frameworks | PHP | Debugger"

https://confluence.jetbrains.com/display/PhpStorm/Debugging+with+PhpStorm

1

The Xdebug Helper extension did the trick, thanks!

0

Please sign in to leave a comment.