如何为命令行 PHP 脚本触发 XDebug 分析器?

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

How to trigger XDebug profiler for a command line PHP script?

phpconfigurationcommand-lineprofilingxdebug

提问by selfawaresoup

XDebug offers the configuration directive "xdebug.profiler_enable_trigger" that allows to activate profiling by passing the GET or POST parameter "XDEBUG_PROFILE" when calling a script via HTTP. This is handy if you don't want profiling for ALL of your scripts but only for a few special cases without always changing your PHP configuration.

XDebug 提供了配置指令“xdebug.profiler_enable_trigger”,当通过 HTTP 调用脚本时,它允许通过传递 GET 或 POST 参数“XDEBUG_PROFILE”来激活分析。如果您不想对所有脚本进行性能分析,而只想对少数特殊情况进行分析而不总是更改 PHP 配置,这将非常方便。

Is there a way to achieve the same behavior for command line PHP programs? I tried to pass the "XDEBUG_PROFILE" as a command line argument but it didn't work.

有没有办法为命令行 PHP 程序实现相同的行为?我试图将“XDEBUG_PROFILE”作为命令行参数传递,但没有奏效。

In general, profiling command line PHP works well, but I'd like to have the same per-call-flexibility as with a browser and HTTP server.

一般来说,分析命令行 PHP 效果很好,但我希望具有与浏览器和 HTTP 服务器相同的每次调用灵活性。

Any suggestions?

有什么建议?

回答by jou

You can pass INI settings with the -dflag: php -d xdebug.profiler_enable=On script.php.

您可以使用以下-d标志传递 INI 设置:php -d xdebug.profiler_enable=On script.php

回答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) 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.php" at the command line.

然后它只是在netbeans中开始调试并在命令行中执行“php myscript.php”的情况。

回答by oliver nadj

with PhpStorm on remote webserver i use this command:

在远程网络服务器上使用 PhpStorm 我使用以下命令:

XDEBUG_CONFIG="idekey=PHPSTORM" PHP_IDE_CONFIG="serverName=server_name" php -dxdebug.remote_host=`echo $SSH_CLIENT | cut -d "=" -f 2 | awk '{print }'` myscript.php

where server_namestands for name of the server in PhpStorm project conifuguration

其中server_name代表 PhpStorm 项目配置中的服务器名称

回答by outis

As described on the Xdebug Remote Debuggingpage, profiling can also be enabled via the XDEBUG_CONFIGenvironment variable by inluding a "profile_enable=1" directive:

Xdebug 远程调试页面所述,还可以通过XDEBUG_CONFIG包含“profile_enable=1”指令通过环境变量启用分析:

XDEBUG_CONFIG="profiler_enable=1" php ...

回答by Joshua Dance

Similar, but different process for getting it to work with Netbeans while developing on a VM.

在 VM 上进行开发时使其与 Netbeans 一起工作的类似但不同的过程。

Need to pass in the remote enabled flag, the auto start flag, the ide flag, and the name of your remote host.

需要传入远程启用标志、自动启动标志、ide 标志和远程主机的名称。

php -dxdebug.remote_enable=1 -dxdebug.remote_autostart=On -dxdebug.idekey=netbeans-xdebug -dxdebug.remote_host=NAME.OF.HOST script.php

回答by user2970583

I created a shell script to handle client debugging.

我创建了一个 shell 脚本来处理客户端调试。

script name: phpdebug

脚本名称:phpdebug

#!/usr/bin/ksh
php -dxdebug.remote_host=`echo $SSH_CLIENT | cut -d "=" -f 2 | awk '{print }'` $*

I placed this script in /usr/binand gave it execute permissions.

我将这个脚本放入/usr/bin并赋予它执行权限。

The script takes the arguments passed into phpdebug and calls php with the xdebug arguments and appends the arguments passed into the shell script, the $* on the end.

该脚本接受传递给 phpdebug 的参数,并使用 xdebug 参数调用 php,并将传递到 shell 脚本的参数附加到最后,$*。

回答by Chukky Nze

In PhpStorm 7 using WAMP I got this to work by copying my already working xdebug settings from C:\wamp\bin\apache\apache2.2.22\bin\php.ini to the xdebug section of C:\wamp\bin\php\phpX.Y.Z\php.ini. Then I ran my script like so:

在使用 WAMP 的 PhpStorm 7 中,我通过将我已经工作的 xdebug 设置从 C:\wamp\bin\apache\apache2.2.22\bin\php.ini 复制到 C:\wamp\bin\php\ 的 xdebug 部分来实现这一点phpX.YZ\php.ini。然后我像这样运行我的脚本:

php -d xdebug.idekey=PHPSTORM script.php

This even worked for debugging laravel artisan scripts

这甚至适用于调试 Laravel 工匠脚本

php -d xdebug.idekey=PHPSTORM artisan db:seed --force

回答by adm1n

Documentation from Jetbrains

来自 Jetbrains 的文档

To start the script with debugging using PHP command line switches Set an environment variable that would tell XDebug to connect to IDE:

使用 PHP 命令行开关通过调试启动脚本 设置一个环境变量,告诉 XDebug 连接到 IDE:

Windows / MacOS / Linux

Windows / MacOS / Linux

export XDEBUG_CONFIG="idekey=123"  

Here idekey should have a random value.

这里 idekey 应该有一个随机值。

Launch PHP with the following command-line options:

使用以下命令行选项启动 PHP:

php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 -dxdebug.remote_connect_back=0 path/to/script.php

You may use 10.0.2.2 instead of 127.0.0.1 with Vagrant (see related SO question).

您可以在 Vagrant 中使用 10.0.2.2 而不是 127.0.0.1(请参阅相关的 SO 问题)。