使用 xdebug 和 netbeans 调试 php-cli 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2944880/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:13:19  来源:igfitidea点击:

Debugging php-cli scripts with xdebug and netbeans?

command-linenetbeansxdebugphp

提问by wurdalack

I have managed to initiate php-cli script debug session from the IDE itself, but I need to start the debugging session from the shell / command line. These are rather complex maintenance PHP scripts which take a lot of input parameters, so entering arguments from within Netbeans is a bit cumbersome.

我已经设法从 IDE 本身启动 php-cli 脚本调试会话,但我需要从 shell/命令行启动调试会话。这些是相当复杂的维护 PHP 脚本,需要大量输入参数,因此从 Netbeans 中输入参数有点麻烦。

I have done it before with Zend studio: https://zend18.zendesk.com/hc/en-us/articles/203838096-Debugging-PHP-CLI-Scripts, but now I need to get it working with Netbeans.

我之前用 Zend studio 做过:https: //zend18.zendesk.com/hc/en-us/articles/203838096-Debugging-PHP-CLI-Scripts,但现在我需要让它与 Netbeans 一起工作。

Thanks in advance.

提前致谢。

回答by Andrew Hancox

I got this working on Ubuntu/Netbeans by:

我通过以下方式在 Ubuntu/Netbeans 上工作:

  • copying the xdebug config lines from the /etc/php5/apache2/php.ini file into /etc/php5/cli/php.ini
  • setting an environment variable with the name of the debug session (you can get this from the query string in the url of the page netbeans launches when you start debugging) so the command is: export XDEBUG_CONFIG="idekey=netbeans-xdebug"
  • 将 xdebug 配置行从 /etc/php5/apache2/php.ini 文件复制到 /etc/php5/cli/php.ini
  • 使用调试会话的名称设置环境变量(您可以从 netbeans 启动调试时启动的页面的 url 中的查询字符串中获取它),因此命令是: export XDEBUG_CONFIG="idekey=netbeans-xdebug"

Then it's simply a case of starting debugging in netbeans and doing php myscript.phpat the command line.

那么这只是一个在 netbeans 中开始调试并php myscript.php在命令行中进行的情况。

Note: If you want to debug remotely using netbeans you need to use Debug File on the file that is being run from the command line, not normal Debug.

注意:如果您想使用 netbeans 远程调试,您需要在从命令行运行的文件上使用调试文件,而不是正常的调试。

回答by johannes

Add xdebug.remote_autostart=Onto your php.ini file or add -dxdebug.remote_autostart=Onas parameter to the PHP binary call (php -d... script.php).

添加xdebug.remote_autostart=On到 php.ini 文件或-dxdebug.remote_autostart=On作为参数添加到 PHP 二进制调用 ( php -d... script.php)。

See http://xdebug.org/docs/remote

请参阅http://xdebug.org/docs/remote

回答by Darwin

I will put all together, the following is that works for me.

我将把所有内容放在一起,以下是对我有用的。

file:
/etc/php5/cli/php.ini

zend_extension="/usr/lib/php5/20121212/xdebug.so" -> xdebug bin path
xdebug.remote_enable=on
xdebug.remote_host=127.0.0.1
xdebug.remote_handler="dbgp"
xdebug.remote_mode="req"
xdebug.remote_port=9000 -> same port configured in netbeans debugging tab
xdebug.idekey="netbeans-xdebug" -> same ide configured in netbeans debuggin tab
xdebug.remote_autostart=1

then, without any other parameter

然后,没有任何其他参数

php script.php

php脚本.php

回答by Marco Del Corto

I had the same problem, my solution was this:

我遇到了同样的问题,我的解决方案是这样的:

  1. Environment: Netbeans 8.2 under windows (apache+php)
  2. Assuming you already have PHP and NetBeans configured to debug code using Xdebug (http://wiki.netbeans.org/HowToConfigureXDebug#Notes_on_Windows_Configuration)
  3. On netbeans create new Configuration (“Project Properties” > “Run configuration” > “New…”
  4. In the new Configuration set Do Not Open web Browser (“Advanced” > “Do Not Open web Browser”)
  5. Set active the new configuration created (drop down in tool bar)
  6. Set breakpoint for debug
  7. Open debug (CTRL+F5)
  8. Open Terminal window (“Tools” > “Open in Terminal”)
  9. Type in terminal: $ export XDEBUG_CONFIG="idekey=netbeans-xdebug" (the value "netbeans-xdebug" must coincide with “Tools” > “Options” > “Debugging” > “Session ID”)
  10. Type in terminal: $ php.exe -f "C:\Apache24\htdocs\www.SiteName\ScriptName.php" -- "Arg1=x&Arg2=y"
  11. Follow debug…
  1. 环境:windows下的Netbeans 8.2(apache+php)
  2. 假设您已经将 PHP 和 NetBeans 配置为使用 Xdebug 调试代码(http://wiki.netbeans.org/HowToConfigureXDebug#Notes_on_Windows_Configuration
  3. 在 netbeans 上创建新配置(“项目属性”>“运行配置”>“新建...”
  4. 在新的配置中设置不要打开网页浏览器(“高级”>“不要打开网页浏览器”)
  5. 激活创建的新配置(工具栏中的下拉菜单)
  6. 设置调试断点
  7. 打开调试 (CTRL+F5)
  8. 打开终端窗口(“工具”>“在终端中打开”)
  9. 输入终端:$ export XDEBUG_CONFIG="idekey=netbeans-xdebug"(值“netbeans-xdebug”必须与“工具”>“选项”>“调试”>“会话ID”一致)
  10. 在终端输入: $ php.exe -f "C:\Apache24\htdocs\www.SiteName\ScriptName.php" -- "Arg1=x&Arg2=y"
  11. 按照调试...

回答by Mike

Make sure you also setup DBGP_IDEKEY value, because usually it is not idekey. For example on Linux:

确保您还设置了 DBGP_IDEKEY 值,因为通常它不是idekey。例如在 Linux 上:

export DBGP_IDEKEY="netbeans-xdebug" 
export XDEBUG_CONFIG="netbeans-xdebug=netbeans-xdebug"

回答by Julian

I had the same problem my solution was this:

我有同样的问题我的解决方案是这样的:

In Netbeans > the project window > right click on the php project > properties > Run configuration. Create a New Configuration.

在 Netbeans > 项目窗口 > 右键单击​​ php 项目 > 属性 > 运行配置。创建新配置。

Fill the correct values:

填写正确的值:

  • Run as "script"
  • set php interpreter
  • change index file in my case it was "cron/index.php".
  • 作为“脚本”运行
  • 设置php解释器
  • 在我的情况下更改索引文件是“cron/index.php”。

回答by Renato Cassino

You can use the Dephpugger project if you dont want to configure xDebug for your IDE (i hate configurations).

如果您不想为您的 IDE 配置 xDebug(我讨厌配置),您可以使用 Dephpugger 项目。

https://github.com/tacnoman/dephpugger

https://github.com/tacnoman/dephpugger

You can run the debugger in terminal, like ipdb for Python and byebug for Ruby.

您可以在终端中运行调试器,例如 Python 的 ipdb 和 Ruby 的 byebug。