Debug PHP command line script in PHPStorm

25,358

Solution 1

Please watch/vote the related PhpStorm issue.

At the moment configuration is a bit complicated. It should be possible to debug like described in the Zend KB, with a fake server and Php Remote Debug configuration in PhpStorm. Note that this tutorial has an error, you need to set QUERY_STRING like this (on Windows):

SET "QUERY_STRING=start_debug=1&debug_port=10137&debug_host=127.0.0.1&debug_stop=1&no_remote=1&debug_session_id=10000"

(note the difference in quoting which is important).

Set up PhpStorm like shown on the screenshots:

fake server

remote debug

Press the Debug button on the main toolbar. PhpStorm will start listening for the debugger connections.

In the command line run your script:

d:\dev\zend\ZendServer\bin\php.exe d:\work\attaches\sample\helps.php

PhpStorm will hit the breakpoint:

breakpoint hit

This was tested on Windows with Zend Server installation, but should be similar on the other systems and with xdebug.

If you want to debug directly from PhpStorm, use Php Script Run/Debug configuration type. Your PHP instance configured for this project must have either Zend Debugger or Xdebug enabled in the configuration files.

PHP home and debugger mode can be specified in File | Settings | PHP.

Just verified with Zend server installation (PHP home: d:\dev\zend\ZendServer\bin) and Zend Debugger, works perfectly for me.

Solution 2

To enable PHP Debuging with Xdebug on the shell/command line with Phpstorm is very easy:

export XDEBUG_CONFIG="idekey=PHPSTORM"

In windows CLI, use set instead of export.

set XDEBUG_CONFIG="idekey=PHPSTORM"

Ensure Phpstorm is listening to the PHP Debug connections.

To disable debugging from command line end:

unset XDEBUG_CONFIG

This (PHPSTORM) is the default Phpstorm IDE-Key.

Solution 3

  1. Click Run.
  2. Click Edit Configurations...
  3. Click the green +
  4. Select PHP Script.
  5. Provide a name in the Name field.
  6. Provide the full path to your script in the File field.
  7. Click OK.
  8. Select the script you just configured in this dropdown:

Screenshot of PhpStorm menu bar

  1. Click the run or debug button. Your script should run and it should catch any breakpoints that are set.

If you run the script, you can see output here:

Screenshot of Run widget, typically docked on the bottom of the window

If you debug the script, you can see output here:

Screenshot of Console widget, docked in the Debug widget

Solution 4

Check that environments variables SERVER_NAME and SERVER_PORT are set. PHPStorm needs them to do the right mapping. Set them with export in the bash or manually in the phpscript. In both cases PHPStorm can find it then under $_SERVER['SERVER_NAME'] and $_SERVER['SERVER_PORT'] correctly.

Solution 5

None of these suggestions were working fine for me.

If you are in the same situation try this command (check the right php interpreter path)

/usr/bin/php -dzend_extension=xdebug.so -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 -dxdebug.remote_autostart=1 [PHP_SCRIPT]
Share:
25,358

Related videos on Youtube

JoshuaDavid
Author by

JoshuaDavid

Full-stack Real-time Modern JavaScript Virtuoso Angular 2/4/5+, ngrx (redux) Node/Feathers mongoDB/Mongoose ngMaterial2, ionic, cordova TypeScript, rxjs, JavaScript ES6/ES7 docker, nginx and so much more...

Updated on July 09, 2022

Comments

  • JoshuaDavid
    JoshuaDavid almost 2 years

    I use PHPStorm as my primary tool for debugging PHP and am very pleased. So far I've been using Firefox to run the scripts, and PHPStorm catches breaks perfectly.

    Now I have a situation where I am running a PHP script via command line (not browser) and want to know if there's a way to setup PHPStorm to catch breaks when running a PHP script via command line?

    • J.D.
      J.D. over 8 years
      See Venkat's answer below for the simple solution. The currently accepted answer is outdated.
  • JoshuaDavid
    JoshuaDavid over 13 years
    +1 Thank you very much! Using the Php Script Run/Debug configuration type is an acceptable workaround -- I tried it and have it working. However, it is less than ideal, as I have to setup a configuration for each script I want to run a 3rd party external tool on. It would be nice if I could use the external tool mechanism and have PHPStorm catch breaks... but I guess I'll have to wait!
  • CrazyCoder
    CrazyCoder over 13 years
    I've updated my reply with the pure remote configuration guide, it didn't work before because of incorrect quoting in the Zend KB article.
  • JoshuaDavid
    JoshuaDavid over 13 years
    FYI, here is a sample environment setting for running XDEBUG, which I am using successfully: ----- set "XDEBUG_CONFIG=idekey=PHPSTORM_XDEBUG remote_enable=1 remote_host=127.0.0.1 remote_port=9000 remote_handler=dbgp remote_mode=req" ----- ...again, quoting seems to be sensitive
  • Alex
    Alex about 10 years
    This sorted out mapping for me, thanks! Although it only worked when I exported the variables in bash - setting them in the PHP script itself didn't work.
  • Jan.J
    Jan.J almost 10 years
    In Windows CLI use set XDEBUG_CONFIG="idekey=PHPSTORM"
  • J.D.
    J.D. over 8 years
    The mentioned issue was fixed in July 2011. The correct answer is Venkat's below.
  • Arth
    Arth almost 8 years
    I don't understand, PHPStorm won't let me run "set" in the command line tools console, 'Cannot run program "set" (in directory "blah/blah/blah/"): CreateProcess error=2, The system cannot find the file specified' and if I do it in CMD, then run the script.. it doesn't break on the breakpoints (I have PHPStorm listening to connections).

Related