在 PHPStorm 中调试 PHP 命令行脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5315247/
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
Debug PHP command line script in PHPStorm
提问by JoshuaDavid
I use PHPStormas 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.
我使用PHPStorm作为我调试 PHP 的主要工具并且非常高兴。到目前为止,我一直在使用 Firefox 来运行脚本,而 PHPStorm 可以完美地捕获中断。
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?
现在我遇到了一种情况,我通过命令行(而不是浏览器)运行 PHP 脚本,想知道是否有办法设置 PHPStorm 以在通过命令行运行 PHP 脚本时捕获中断?
采纳答案by CrazyCoder
Please watch/votethe related PhpStorm issue.
请观看/投票相关的 PhpStorm 问题。
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 Debugconfiguration in PhpStorm. Note that this tutorial has an error, you need to set QUERY_STRING like this (on Windows):
目前配置有点复杂。应该可以像Zend KB 中描述的那样进行调试,在 PhpStorm 中使用假服务器和Php 远程调试配置。请注意,本教程有一个错误,您需要像这样设置 QUERY_STRING(在 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:
按照屏幕截图所示设置 PhpStorm:
Press the Debugbutton on the main toolbar. PhpStorm will start listening for the debugger connections.
按主工具栏上的调试按钮。PhpStorm 将开始侦听调试器连接。
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:
PhpStorm 会遇到断点:
This was tested on Windows with Zend Server installation, but should be similar on the other systems and with xdebug.
这是在安装 Zend Server 的 Windows 上进行的测试,但在其他系统和 xdebug 上应该类似。
If you want to debug directly from PhpStorm, use Php ScriptRun/Debug configuration type. Your PHP instance configured for this project must have either Zend Debuggeror Xdebugenabled in the configuration files.
如果要直接从 PhpStorm 调试,请使用Php ScriptRun/Debug 配置类型。为此项目配置的 PHP 实例必须在配置文件中启用Zend Debugger或Xdebug。
PHP home and debugger mode can be specified in File | Settings | PHP
.
PHP home 和调试器模式可以在File | Settings | PHP
.
Just verified with Zend server installation (PHP home: d:\dev\zend\ZendServer\bin
) and Zend Debugger, works perfectly for me.
刚刚通过 Zend 服务器安装(PHP 主页d:\dev\zend\ZendServer\bin
:)和 Zend Debugger 验证,对我来说非常适合。
回答by Venkat Kotra
To enable PHP Debuging with Xdebug on the shell/command line with Phpstorm is very easy:
使用 Phpstorm 在 shell/命令行上使用 Xdebug 启用 PHP 调试非常简单:
export XDEBUG_CONFIG="idekey=PHPSTORM"
In windows CLI, use set instead of export.
在 Windows CLI 中,使用 set 而不是 export。
set XDEBUG_CONFIG="idekey=PHPSTORM"
Ensure Phpstorm is listening to the PHP Debug connections.
确保 Phpstorm 正在侦听 PHP 调试连接。
To disable debugging from command line end:
要从命令行端禁用调试:
unset XDEBUG_CONFIG
This (PHPSTORM) is the default Phpstorm IDE-Key.
这个 (PHPSTORM) 是默认的 Phpstorm IDE-Key。
回答by David Winiecki
- Click Run.
- Click Edit Configurations...
- Click the green +
- Select PHP Script.
- Provide a name in the Name field.
- Provide the full path to your script in the File field.
- Click OK.
- Select the script you just configured in this dropdown:
- 单击运行。
- 单击编辑配置...
- 点击绿色+
- 选择 PHP 脚本。
- 在名称字段中提供名称。
- 在“文件”字段中提供脚本的完整路径。
- 单击确定。
- 在此下拉列表中选择您刚刚配置的脚本:
- 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:
如果您运行该脚本,您可以在此处看到输出:
If you debug the script, you can see output here:
如果您调试脚本,您可以在此处看到输出:
回答by Peter
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.
检查环境变量SERVER_NAME
和SERVER_PORT
设置。PHPStorm 需要它们进行正确的映射。在 bash 中使用 export 或在 phpscript 中手动设置它们。在这两种情况下PHPStorm可以找到它然后在$_SERVER['SERVER_NAME']
和$_SERVER['SERVER_PORT']
正确。
回答by Rafa? Mnich
For me (on MacOS) it worked that way:
export XDEBUG_CONFIG="idekey=12345"
对我来说(在 MacOS 上)它是这样工作的:
export XDEBUG_CONFIG="idekey=12345"
回答by WonderLand
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)
如果您处于相同的情况,请尝试此命令(检查正确的 php 解释器路径)
/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]
回答by Haris ur Rehman
On MacOS, I was using different bash_profile. I had to run following command first:
在 MacOS 上,我使用了不同的 bash_profile。我必须先运行以下命令:
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.
这将我的 php 从内置的 /usr/bin/php 更改为通过 XAMPP 安装的那个。PHPStorm 的 XDebug 在上述命令后开始工作。