PhpStorm + Xdebug 远程调试卡住了

PhpStorm + Xdebug remote debugging froze

我已经设置 PHP 在 PhpStorm 上使用 Xdebug 调试以命中本地主机。 "smart PHP listening" 或手动调试 运行 一切正常,除非我已经到了代码看起来像

的地步
$aOptions = array (
        'http' => array (
                'header' => "Content-Type: application/x-www-form-urlencoded\r\n$sBits",
                'method' => 'POST',
                'content' => http_build_query ( $aData )
        )
);

$rContext = stream_context_create ( $aOptions );
$sResult = file_get_contents ( $sUrl, false, $rContext );
return \json_decode ( $sResult );

然后卡在了一条线上

$sResult = file_get_contents ( $sUrl, false, $rContext );

带有错误消息

file_get_contents(http://localhost:8888/data/?/Ajax/&q[]=/0/): failed to open stream: HTTP request failed!

但是当我 运行 调试之后那一行将通过所有工作正常。

PhpStorm 和 Xdebug 已经设置好

xdebug.remote_autostart = 1

任何线索为什么 Xdebug 在没有调试的情况下挂起它可以通过 w/o 任何问题?

您也在 file_get_contents() 调用中调用了 localhost。是同一个端口吗?如果 xdebug 也在那里启动,那么该请求将被保留(不会完成)并且最终 file_get_contents 将超时。

是否也可以从 PHP Storm 访问该代码?如果是这样,你应该跳进去。否则尝试 运行 cookie 触发的 xdebug,因此只有主请求触发它。

xdebug.remote_autostart = 1 表示 当此设置设置为 1 时,Xdebug 将始终尝试启动远程调试会话并尝试连接到客户端,即使 GET/POST/COOKIE变量不存在。[1] 这表明它可能是这个

我想你只是想 xdebug.remote_enable = 1 然后使用你的浏览器的 extension/plugin (大多数浏览器都有一个 enable/disable xdebug 插件)只在你的主要请求上触发 xdebug

[1] https://xdebug.org/docs/remote

根据 simultaneous debugging sessions 的 JetBrains 文档,我能够通过添加建议代码来修复它,以启动子请求的调试器会话,如下所示

$aOptions = array (
        'http' => array (
                'header' => "Content-Type: application/x-www-form-urlencoded\r\n$sBits",
                'method' => 'POST',
                'content' => http_build_query ( $aData )
        )
);

$debuggingQuerystring = '';
if (isset($_GET['XDEBUG_SESSION_START'])) { // xdebug
     $debuggingQuerystring = '?XDEBUG_SESSION_START=' . $_GET['XDEBUG_SESSION_START'];
}
if (isset($_COOKIE['XDEBUG_SESSION'])) { // xdebug (cookie)
     $debuggingQuerystring = '?XDEBUG_SESSION_START=PHPSTORM';
}

$rContext = stream_context_create ( $aOptions );
$sResult = file_get_contents ( $sUrl.$debuggingQuerystring, false, $rContext );
return \json_decode ( $sResult );