【问题标题】:XDebug session in PhpStorm stuck on curl_exec callPhpStorm 中的 XDebug 会话卡在 curl_exec 调用上
【发布时间】:2015-08-25 19:42:21
【问题描述】:

我有以下简化的 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 来做到这一点?

【问题讨论】:

    标签: php phpstorm xdebug


    【解决方案1】:

    这在 PhpStorm 中非常简单,只需转到 Settings -> PHP -> Debug 并在 External Connections 部分增加 Max 同时连接数到您需要的数字 - 在我的情况下,2 就足够了。

    【讨论】:

    • 有人知道它在 netbeans 上是如何工作的吗?
    【解决方案2】:

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

    这在 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 浏览器 (*)。不要在调试器代理下设置主机。保存这些设置。在项目窗口中,在您的项目上:鼠标右键单击 > 调试(这将开始侦听调试连接)。没有启动浏览器。在浏览器中输入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

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    • 我更新了我的答案,不确定是否可以从其他人的答案中复制这么多,但现在已经完成了
    • 同意,但似乎还没有答案。我看过——我相信——每一次提到这个问题。没有实际的解决方案,一个以 OP 结尾说“无视,想通了”。我认为有几个与 curl、xdebug 和 phpstorm 相关的不同问题。其中一些是正确设置各种配置的问题 - 这是发布的链接将提供帮助的地方。他们帮助我将其缩小为 phpstorm/xdebug/curl 中的错误或我(和许多其他人)尝试从/到同一服务器建立 curl 连接的一些逻辑谬误。
    • @Alex Tartan,我已经更新了我的答案。
    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 2014-01-16
    • 2021-03-13
    • 2016-12-30
    • 1970-01-01
    • 2013-09-29
    • 2014-09-29
    • 2021-04-12
    相关资源
    最近更新 更多