开发者

Debug PHP command line script in PHPStorm

开发者 https://www.devze.com 2023-02-17 12:17 出处:网络
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.

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 开发者_JAVA技巧way to setup PHPStorm to catch breaks when running a PHP script via command line?


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.


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:

Debug PHP command line script in PHPStorm

Debug PHP command line script in PHPStorm

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:

Debug PHP command line script in PHPStorm

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.


  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:

Debug PHP command line script in PHPStorm

  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:

Debug PHP command line script in PHPStorm

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

Debug PHP command line script in PHPStorm


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.


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]


For me (on MacOS) it worked that way: export XDEBUG_CONFIG="idekey=12345"


On MacOS, I was using different bash_profile. I had to run following command first:

source ~/.bash_profile

This changed my php from built-in /usr/bin/php to the one installed via XAMPP. The PHPStorm's XDebug started working after above command.

0

精彩评论

暂无评论...
验证码 换一张
取 消