eclipse 使用 XDebug 跟踪 PHP Web 服务页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2045316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using XDebug to trace a PHP web service page
提问by Traveling Tech Guy
I'm using Eclipse and XDebug to develop a PHP application that relies on web services. I have test pages that consume my services in 2 ways: AJAX (using jQuery) and cURL.
我正在使用 Eclipse 和 XDebug 来开发一个依赖于 Web 服务的 PHP 应用程序。我有以两种方式使用我的服务的测试页面:AJAX(使用 jQuery)和 cURL。
I add breakpoints to my service page and launch the debugger. When I call the the service from AJAX, execution stops nicely at the breakpoint, and I get my variables, step-by-step control etc.
我将断点添加到我的服务页面并启动调试器。当我从 AJAX 调用服务时,执行在断点处很好地停止,并且我得到了我的变量、逐步控制等。
But when I call the service using cURL (i.e. from within a PHP page), the breakpoints fail to function. Even if I turn on the "Break at first line" debugger option, I cannot get the execution to stop when using cURL.
但是,当我使用 cURL(即从 PHP 页面中)调用该服务时,断点无法运行。即使我打开“在第一行中断”调试器选项,在使用 cURL 时也无法停止执行。
Is it a debugger behavior? Do I need to add a hearder to my cURL calls? Alter the URL? Or is it an XDebug limitation?
这是调试器行为吗?我需要在 cURL 调用中添加一个听者吗?修改网址?还是 XDebug 限制?
Thanks for your time and effort, Guy
感谢您的时间和努力,盖伊
回答by janpio
I can't comment yet, so I post this as an answer.
我还不能发表评论,所以我把它作为答案发布。
Can you debug more than one AJAX request in one session? Was your debug session still running in Eclipse when you tried to debug using cURL?
您可以在一个会话中调试多个 AJAX 请求吗?当您尝试使用 cURL 进行调试时,您的调试会话是否仍在 Eclipse 中运行?
Description on how it works for me:
关于它如何对我来说有效的描述:
- Start debug session with a simple debug.php file that contains only a
<?php
and nothing else. It stops on the first line, you "continue" it and it finishes execution. - Now request the script using cURL (or another browser) adding ?XDEBUG_SESSION_START=ECLIPSE_DBGP to its path (I even think this addition is optional)
- Your script should show up in the debug view stopped at the first line
- 使用一个仅包含 a
<?php
而没有其他内容的简单 debug.php 文件启动调试会话。它停在第一行,您“继续”它并完成执行。 - 现在使用 cURL(或其他浏览器)请求脚本,将 ?XDEBUG_SESSION_START=ECLIPSE_DBGP 添加到其路径(我什至认为这个添加是可选的)
- 您的脚本应显示在停止在第一行的调试视图中
Hope ths helps.
希望这有帮助。
回答by Gleb Esman
Here is tip on how to trigger Xdebugger client from Curl without browser:
以下是有关如何在没有浏览器的情况下从 Curl 触发 Xdebugger 客户端的提示:
1- From command line:
1-从命令行:
curl -H "Cookie: XDEBUG_SESSION=1" http://YOUR-SITE.com/your-script.php
2- From PHP
2- 来自 PHP
<?php
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, 'http://YOUR-SITE.com/your-script.php');
curl_setopt ($ch, CURLOPT_COOKIE, 'XDEBUG_SESSION=1');
curl_exec ($ch);
?>
So it doesn't matter if you attach "XDEBUG_SESSION=1" to CURL URL, but what is necessary is to send a proper cookie together with request.
因此,是否将“XDEBUG_SESSION=1”附加到 CURL URL 无关紧要,但必要的是将适当的 cookie 与请求一起发送。
回答by Javadecaf
I know this is a pretty old thread, but I thought I'd post my experience for others that may come across it, like I did, with the same problem. What I discovered is that, if you are debugging remotely (which I always do), there are a couple settings you have to change in php.ini to make this work. Here are the ones that worked for me:
我知道这是一个很旧的线程,但我想我会像我一样将我的经验发布给其他人,就像我一样,遇到同样的问题。我发现,如果您进行远程调试(我总是这样做),您必须在 php.ini 中更改一些设置才能使其正常工作。以下是对我有用的:
xdebug.remote_connect_back = false
xdebug.remote_host = {client host name or IP}
The first setting is normally "true," and tells xdebug to look for the client at the same IP address where the HTTP request originated. In this case however, the request is coming from the server, so that won't work. Instead you must use the second setting to tell xdebug where to find the client. Hope this helps save somebody a little time!
第一个设置通常是“true”,并告诉 xdebug 在发起 HTTP 请求的同一 IP 地址上查找客户端。但是,在这种情况下,请求来自服务器,因此无法正常工作。相反,您必须使用第二个设置来告诉 xdebug 在哪里可以找到客户端。希望这有助于节省一些人的时间!
回答by Tish
To trigger the debugger the simplest solution is to use the cookie approach -b XDEBUG_SESSION=ECLIPSE_DBGP
worked for me on eclipse, see below:
要触发调试器,最简单的解决方案是使用-b XDEBUG_SESSION=ECLIPSE_DBGP
在 eclipse 上对我来说有效的 cookie 方法,见下文:
curl -H 'Content-type: application/json' \
-b XDEBUG_SESSION="ECLIPSE_DBGP" \
-X POST \
-d '{"uid":200, "message":"asdsad","message_type":1}'
http://daxuebao.local:8083/api/message/send
回答by Pascal MARTIN
When you are debugging the Ajax request, that one is sent by the browser, in the same navigation context as the other (non-Ajax)requests -- which is why it works fine.
当您调试 Ajax 请求时,该请求由浏览器在与其他(非 Ajax)请求相同的导航上下文中发送——这就是它工作正常的原因。
The request sent by curl is in another, different, context -- and I'm not sure you can hook the debugger into that... But, maybe...
curl 发送的请求在另一个不同的上下文中——我不确定你能把调试器挂到那个......但是,也许......
First of all, here's an information that might prove helpful, quoting the Xdebug's documentation:
首先,引用Xdebug 的文档,这里有一条可能有用的信息:
Xdebug contains functionality to keep track of a debug session when started through a browser: cookies. This works like this:
- When the URL variable
XDEBUG_SESSION_START=name
is appended to an URL Xdebug emits a cookie with the name "XDEBUG_SESSION
" and as value the value of theXDEBUG_SESSION_START
URL parameter.- When there is a GET (or POST) variable
XDEBUG_SESSION_START
or theXDEBUG_SESSION
cookie is set, Xdebug will try to connect to a debugclient.- To stop a debug session (and to destroy the cookie) simply add the URL parameter
XDEBUG_SESSION_STOP
. Xdebug will then no longer try to make a connection to the debugclient.
Xdebug 包含在通过浏览器启动时跟踪调试会话的功能:cookie。这像这样工作:
- 当 URL 变量
XDEBUG_SESSION_START=name
附加到 URL 时,Xdebug 会发出一个名为“XDEBUG_SESSION
”的 cookie ,并将XDEBUG_SESSION_START
URL 参数的值作为值。- 当存在 GET(或 POST)变量
XDEBUG_SESSION_START
或XDEBUG_SESSION
设置了 cookie 时,Xdebug 将尝试连接到调试客户端。- 要停止调试会话(并销毁 cookie),只需添加 URL 参数
XDEBUG_SESSION_STOP
。Xdebug 将不再尝试连接到调试客户端。
Maybe it might work if you set that cookie "by hand", sending it allong the curl request...
如果您“手动”设置该 cookie,并沿着 curl 请求发送它,也许它可能会起作用...
I suppose you'd first have to get its value, as set by Xdebug at the beginning of the debugging session -- re-using the cookie you have in your browser should be possible, though.
我想您首先必须获得它的值,正如 Xdebug 在调试会话开始时设置的那样——不过,重新使用浏览器中的 cookie 应该是可能的。
Note : I've never tried this -- if you try, and it works, could you please confirm it worked ?
注意:我从来没有尝试过这个——如果你尝试过,并且它有效,你能确认它有效吗?
回答by matt
I ran into this same exact issue. I solved it by turning the auto-start feature offin php.ini:
我遇到了同样的问题。我通过关闭php.ini 中的自动启动功能解决了这个问题:
xdebug.remote_autostart = 0
and then adding the API key to the webservice URL that my webservice client calls:
然后将 API 密钥添加到我的 webservice 客户端调用的 webservice URL:
?XDEBUG_SESSION_START=<your API key here>
and I'm not sure if this matters, but I entered the API key into my debugger (MacGDBp). Now the debugger fires up only when the webervice server-side script is called, not when the client is started.
我不确定这是否重要,但我将 API 密钥输入到我的调试器 (MacGDBp) 中。现在调试器仅在调用 webervice 服务器端脚本时启动,而不是在客户端启动时启动。
Hope this helps.
希望这可以帮助。