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

Viewed 102118

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.

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.

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.

10 Answers

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

with PhpStorm on remote webserver i use this command:

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

where server_name stands for name of the server in PhpStorm project conifuguration

Documentation from Jetbrains

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

Windows / MacOS / Linux

export XDEBUG_CONFIG="idekey=123"  

Here idekey should have a random value.

Launch PHP with the following command-line options:

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).

For Xdebug 3

Now enabling Xdebug for a single script would easily be accomplished using the XDEBUG_MODE environment variable:

XDEBUG_MODE=profile php script_to_be_profiled.php

Welcome to xdebug 3!

It is now:

xdebug.mode=profile

therefore:

php -d xdebug.mode=profile script.php
Related