XDebug and RESTful server using PHPStorm or POSTman

RestXdebugPhpstorm

Rest Problem Overview


How can I get a REST client (such as the one built into PHPStorm or POSTman) to work with XDebug?

In my current set-up of XDebug, using PHPStorm and the Bookmarklet provided I'm able to get it working in both Chrome and Firefox - but as soon as I try with POSTman or any other REST client, I can't figure out how to get it started.

Cheers.

Rest Solutions


Solution 1 - Rest

You can use one of these approaches:

  1. Configure your Xdebug (by editing php.ini) to attempt to debug every PHP script. The key option:

    • Xdebug v2: xdebug.remote_autostart = 1
    • Xdebug v3: xdebug.start_with_request = yes
  2. Add Xdebug session start parameter to the actual URL (XDEBUG_SESSION_START={{KEY}} -- https://xdebug.org/docs/step_debug#manual-init), for example: ?XDEBUG_SESSION_START=PHPSTORM

  3. Pass Xdebug cookie as part of the request (the one which is set by bookmarklet or browser extension, for example).

For this to work: make sure that "phone handle" icon is activated in advance in PhpStorm (Run | Start Listen for PHP Debug Connection).


P.S. If you are using Postman, Insominia or alike (another REST client) then the best / most transparent way IMO is to use Xdebug cookie. You're most likely already using separate Environments (e.g. "dev", "test", "production") so you can have such a cookie only where it is needed (depends on the tool and version used of course).

This way there is no need to edit the URL (even if you have it as a "conditional parameter" that is present for some environment and absent for another) or configure Xdebug to "debug all requests" at all.

An example of such Xdebug cookie from my Postman (edit it as needed; here it is set for the local some-domain.local.test fake domain):

XDEBUG_SESSION=value; Path=/; Domain=.some-domain.local.test; Expires=Tue, 19 Jan 2038 03:14:07 GMT;

Since the host URL should be a part of your Environment (e.g. the endpoint URL will be like {{host}}/api/v1/welcome) then such cookie will be sent to the dev domain only and not to the production one.

Solution 2 - Rest

Just add ?XDEBUG_SESSION_START=filter_string at the end of the url, for eg:

https://new-supplier.local/api/login?XDEBUG_SESSION_START=PHPSTORM

PHPSTORM is my default filter string, you can use whatever you want. Your editor should be set up to filter connections by IDE key (filter string), and thats it. You should be able to debug the same way as from Chrome or FF.

Solution 3 - Rest

Warning!

xdebug >= 3.0 has changed the parameters in php.ini. After upgrading xdebug, most of the answers here will not be relevant.

Refer to: https://xdebug.org/docs/upgrade_guide

Basically, you need to add something like this to your php.ini:

xdebug.mode=develop,gcstats,coverage,profile,debug
xdebug.start_with_request=1
xdebug.idekey=PHPSTORM

Solution 4 - Rest

This was driving me crazy. I just updated to PHP 7.1 and xdebug that was working no longer worked. I updated the xdebug.so file (Linux) and php --version indicated that xdebug was indeed being loaded and working. But when I would use Postman the debugger never kicked on.

Here's the solution. If you are using Apache as your server then you need to enable the PHP 7.1 mods and reboot Apache: sudo service apache2 restart

Solution 5 - Rest

xdebug.remote_timeout = 60000

Worked for me. As my Mac was very slow, and Remote debugger was timed out after 200 ms (Default value)

Solution 6 - Rest

What finally got my Postman/PHPStorm Xdebug working was adding a PHP Remote Debug configuration in PHPStorm:

Run -> Edit Configurations -> + -> PHP Remote Debug

I just set the name to localhost and saved it - no IDE Key, etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel HollandsView Question on Stackoverflow
Solution 1 - RestLazyOneView Answer on Stackoverflow
Solution 2 - RestAleksandar StojilkovicView Answer on Stackoverflow
Solution 3 - ResttsaneckiView Answer on Stackoverflow
Solution 4 - RestRyanNerdView Answer on Stackoverflow
Solution 5 - RestNikunj AcharyaView Answer on Stackoverflow
Solution 6 - RestKevinView Answer on Stackoverflow