PhpStorm 中的 XDebug 会话卡在 curl_exec 调用上

XDebug session in PhpStorm stuck on curl_exec call

我有以下简化的API:

http://localhost/app/apiA
http://localhost/app/apiB

其中 apiA 进行一些处理,然后执行这些简单的操作,以便 apiA 调用 apiB:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, site_url('apiB'));
//various other options
$response = curl_exec($curl);

现在,我在 PhpStorm 中的 curl_exec 调用 apiA 中放置了一个断点,在 apiB 方法的第一行放置了另一个断点。发生的情况是,首先,XDebug 卡在 curl_exec 调用上并将无限期地保留在那里;但是,如果我按下 Break,停止解释器,XDebug 会中断 会激活我在 apiB!

中的断点

我希望它执行对 curl_exec 的调用并在 apiB 中命中断点,然后在完成后返回到第一个断点。有什么方法可以配置 XDebug and/or PhpStorm 来执行此操作?

这在 PhpStorm 中非常简单,只需转到设置 -> PHP -> 调试并在 外部连接 部分增加 最大同时连接数 到您需要的数量 - 在我的情况下,2 个就足够了。

我终于让它可靠地工作了。所以我会修改我的答案。我意识到我的答案与我在其他地方看到的基本相同,但不知何故我没有理解它们,所以我会尽量说得更清楚。

这在 phpstorm 9.5 和 10 上进行了测试,但很可能在早期版本上也能正常工作。我使用 Linux,(Kubuntu 14.04)。 (假设xdebug已经在phpstorm中正常运行了。)

设置是我从命令行上的 curl 启动会话,然后请求由我的应用程序中的路由 (routeA) 处理,该路由使用 cURL (ie. curl_exec())。然后将结果 returned 到 routeA,最后 returned 到命令行。

问题:要在 phpstorm/xdebug 中通过整个 request/response 周期进行全面调试。

为了使其正常工作,需要在 xdebug 中进行以下设置。必须设置 xdebug 来处理远程调试会话,并且必须有一个 ide 键可用于触发调试会话 - 我没有找到其他似乎相关的设置。更改后重新启动服务器。

xdebug.remote_enable=1
xdebug.idekey=PHPSTORM

其次,在phpstorm中,你需要允许多个外部同时连接。我相信 cURL 调用(从一个路由到另一个路由)被视为外部的,因为第一个连接在等待第二个到 return 时保持活动状态,如果链是,至少需要 2 个可能更多更长。这个设置可以在设置中找到

Language & Frameworks > php > Debug > External Connections

最后你需要告诉 phpstorm 去听。你可以在几个地方找到这个选项,一个地方在 运行 菜单中(在版本 10 中它在底部)

Run > Start Listening for PHP Debug Connections

然后在命令行中使用 PHPSTORM ide 键触发调试:

curl -i -v http://yoursite/routea/?XDEBUG_SESSION_START=PHPSTORM

在 routeA 中,您像这样创建 cURL 调用,您

public function routeaAction()
{
    ...

    //initialize the curl object
    $curl = curl_init("http://yoursite/routeb/");
    // trigger the second debug connection by setting a special cookie
    curl_setopt($curl, CURLOPT_COOKIE,"XDEBUG_SESSION=PHPSTORM");

    // debugging options
    // short timeout, stop on errors, show headers, be verbose etc.
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_FAILONERROR,true);
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    curl_setopt($curl, CURLINFO_HEADER_OUT,true);
    curl_setopt($curl, CURLOPT_TIMEOUT,2);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);

    // follow along to the second route and the return the result
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    // if your site is a virtual host - typical on your local dev machine
    // you need to tell your web server which site (virtual host) you want
    // the url is not enough - you need to set the Host header
    // there may be other headers you want to set - do it like this
    $requestHeaders[] = "Host: yoursite";
    $requestHeaders[] = "Cache-Control: no-cache";
    $requestHeaders[] = "Pragma: no-cache";
    curl_setopt($curl, CURLOPT_HTTPHEADER,$requestHeaders);

    // finally you're ready to send the request
    $data = curl_exec($curl);
    $errno = curl_errno($curl)
    if ($errno){
        $data .= 'Error: ' . curl_error($curl) ."\n";
    }
    curl_close($curl);

    return $data;
}

我将保留 link,因为它们很有用,但从中删除我的引述,因为它们不是必需的。

这个例子很好地说明了如何为 cURL 设置选项:

https://whosebug.com/users/3813605/misunderstood

此 link 解释了 windows

下的一些 xdebug.ini 设置

Curl with XDebug on NetBeans

[XDebug]
zend_extension = "C:\xampp\php\ext\php_xdebug-2.2.5-5.4-vc9.dll"
xdebug.profiler_append = 0
xdebug.profiler_enable = 1
xdebug.profiler_enable_trigger = 0 
xdebug.profiler_output_dir = "C:\xampp\tmp"
xdebug.profiler_output_name = "cachegrind.out.%t-%s"
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_autostart = 1
xdebug.remote_host = "127.0.0.1"
xdebug.remote_port = "9000"
xdebug.trace_output_dir = "C:\xampp\tmp"
xdebug.max_nesting_level = 200
xdebug.idekey = "netbeans-xdebug"

这个 link 有一些特定于 netbeans 的信息,但也可能适用于 phpstorm

Launch XDebug in Netbeans on an external request

go to project properties > run configuration > advanced > debug url and check do not open web browser (*). do not set the host under debugger proxy. save these settings. in the project window, on your project: right mouse click > debug (this starts listening for debug connections). no browser is started. enter http://www.mywebsite.com?XDEBUG_SESSION_START=netbeans-xdebug in your browser. it should break in netbeans. at least that's what happens here :)

404 not found in curl