PhpStorm中的XDebug会话停留在curl_exec调用上

时间:2015-08-25 19:42:21

标签: php phpstorm xdebug

我有以下简化的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调用apiAapiB方法的第一行右边放置一个断点。会发生什么事情,首先,XDebug卡在curl_exec电话上并将无限期地留在那里;但是如果我按下Break,停止翻译,XDebug会打开,但会在apiB中激活我的断点!

我希望它执行对curl_exec的调用并点击apiB中的断点,然后在完成后返回第一个断点。有没有办法配置XDebug和/或PhpStorm来做到这一点?

2 个答案:

答案 0 :(得分:15)

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

Screenshot of settings

答案 1 :(得分:6)

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

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

设置是我在命令行上从curl启动会话,然后请求由我的应用程序中的路由(routeA)处理,该路由使用cURL向另一个路由(routeB)发送新请求(即curl_exec( ))。然后将结果返回到routeA,最后返回到命令行。

问题:在整个请求/响应周期中在phpstorm / xdebug中进行完全调试。

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

xdebug.remote_enable=1
xdebug.idekey=PHPSTORM

其次,在phpstorm中,您需要允许多个外部同时连接。我认为cURL调用(从一个路由到另一个路由)被视为外部,在第一个连接等待第二个返回时保持活动的意义上,如果链更长,则至少需要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;
}

我会留下链接,因为它们很有用,但删除了我的引用,因为它们不是必需的。

这个给出了如何为cURL设置选项的一个很好的例子:

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

此链接解释了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"

此链接有一些特定于netbeans的信息,但也可能适用于phpstorm

Launch XDebug in Netbeans on an external request

  

转到项目属性>运行配置>高级>调试url并检查不要打开Web浏览器(*)。不要在调试器代理下设置主机。保存这些设置。在项目窗口中,在您的项目上:右键单击> debug(这开始监听调试连接)。没有浏览器启动。在浏览器中输入http://www.mywebsite.com?XDEBUG_SESSION_START=netbeans-xdebug。它应该打破netbeans。至少这是在这里发生的事情:)

FileNotFoundException with 404 status for valid URL on HTTP GET request

404 not found in curl