如何使用 xdebug 调试 PHP CLI 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1947395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I debug a PHP CLI script with xdebug?
提问by Laran Evans
I haven't quite figured this out. EVERY piece of documentation I've found covers how to use xdebug to debug scripts running in Apache. I need to debug a php CLI script.
我还没有完全弄清楚这一点。我找到的每篇文档都涵盖了如何使用 xdebug 调试在 Apache 中运行的脚本。我需要调试一个 php CLI 脚本。
So, for instance, how do I pass the XDEBUG_SESSION_START variable in to get xdebug to kick on?
因此,例如,我如何传递 XDEBUG_SESSION_START 变量以启动 xdebug?
I'm specifically trying to debug a CakePHP shell. So if anyone has any additional insight into that I'd be very appreciative.
我特别想调试 CakePHP shell。因此,如果有人对此有任何其他见解,我将不胜感激。
Thanks.
谢谢。
回答by Pascal MARTIN
There is a couple of notes about that in Xdebug's manual, like, for instance (quoting) :
Xdebug 的手册中有一些关于它的注释,例如(引用):
export XDEBUG_CONFIG="idekey=session_name"
php myscript.php
If you are using Eclipse PDT to develop and debug your PHP scripts, there is not much difference between Apache or CLI : the configuration lloks quite the same, you just don't have to configure a web server, nor indicate an URL ; instead, you have to indicate the path to the PHP executable.
如果您使用 Eclipse PDT 来开发和调试 PHP 脚本,Apache 或 CLI 之间没有太大区别:配置完全相同,您只是不必配置 Web 服务器,也不必指明 URL;相反,您必须指明 PHP 可执行文件的路径。
About the XDEBUG_SESSION_STARTvariable : well, you launch the whole script in "debug-mode", so you don't have any notion of "debugging-session", I'd say.
关于XDEBUG_SESSION_START变量:嗯,你在“调试模式”下启动整个脚本,所以你没有任何“调试会话”的概念,我想说。
For instance, here's what Window > Preference > PHP > PHP executableslooks like for me right now, and, on the right, what I get when clicking on the Editbutton of the first one :
例如,这是Window > Preference > PHP > PHP executables我现在的样子,在右边,我点击Edit第一个按钮时得到的结果:

(source: pascal-martin.fr)

(source: pascal-martin.fr)

(来源:pascal-martin.fr)(来源:pascal-martin.fr)

And the debug configurationswindow :
和debug configurations窗口:

(source: pascal-martin.fr)

(来源:pascal-martin.fr)
And launching the debugging: it just works :
并启动调试:它只是有效:

(source: pascal-martin.fr)

(来源:pascal-martin.fr)
Hope this helps :-)
希望这可以帮助 :-)
Else, what specific problem do you encounter ?
否则,您遇到什么具体问题?
回答by Plamen
If you're using bash (or similar shell), this little script might come in handy:
如果您使用 bash(或类似的 shell),这个小脚本可能会派上用场:
alias drush-debug=drd
function drd {
export XDEBUG_CONFIG="idekey=cli_session"
export SERVER_NAME="developer.machine"
export SERVER_PORT="9000"
drush "$@"
unset XDEBUG_CONFIG
unset SERVER_NAME
unset SERVER_PORT
};
or as suggested by the commentators below
或按照以下评论员的建议
alias drd='XDEBUG_CONFIG="idekey=PHPSTORM" drush "$@"'
This way you don't have to manually set and unset the trigger variable each time you want to debug.
这样您就不必在每次要调试时手动设置和取消设置触发器变量。
回答by Niels
simply put the following section to your php.ini
只需将以下部分放入您的 php.ini
[XDebug]
xdebug.max_nesting_level = 200
xdebug.remote_enable=1
xdebug.remote_port=9000
;xdebug.profiler_enable=1
xdebug.idekey=PHPSTORM
xdebug.remote_autostart=1
and replace PHPSTORM with your ide key
并用您的 ide 密钥替换 PHPSTORM
回答by 8ctopus
For Windows and Visual Studio Code here's how to proceed:
对于 Windows 和 Visual Studio Code,这里是如何进行的:
Download xdebug from https://xdebug.org/download. For example php 7.4 Windows 64bit https://xdebug.org/files/php_xdebug-2.9.5-7.4-vc15-nts-x86_64.dll
Copy the xdebug dll to your php extensions dir (ext).
Add to the end of php.ini
从https://xdebug.org/download下载 xdebug 。例如 php 7.4 Windows 64bit https://xdebug.org/files/php_xdebug-2.9.5-7.4-vc15-nts-x86_64.dll
将 xdebug dll 复制到您的 php 扩展目录 (ext)。
添加到php.ini的末尾
[XDebug]
zend_extension=php_xdebug-2.9.5-7.4-vc15-nts-x86_64.dll
xdebug.remote_enable=1
xdebug.remote_autostart=1
Open VSCode and install https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug
Open the project workspace in VSCode, go to Run tab, click the cogwheel and add these lines
打开 VSCode 并安装https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug
在 VSCode 中打开项目工作区,转到运行选项卡,单击齿轮并添加这些行
{
"name": "listen CLI",
"type": "php",
"request": "launch",
"port": 9000
},
{
"name": "run CLI",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
Place a break point in the script you want to debug
Select "run CLI" and click "Start Debugging"
在要调试的脚本中放置断点
选择“运行 CLI”并单击“开始调试”
Happy debugging!
调试愉快!

