在 php 中调试被调用的 web 服务的最佳方法?
我有一个 n 层系统,其中前端模板层调用后端应用程序服务器。 例如,我需要检索一些搜索结果,前端调用后端来获取结果。
模板引擎和应用程序服务器都是用 PHP 编写的。 我目前使用 PHPed 启动到模板引擎的调试会话,但是,当 http 请求发送到远程服务时,我的调试器只是等待 IO 完成。
我想做的是模拟 HTTP 调用,但实际上只是留在我的 PHP 进程中,将环境巨大地推送到某种堆栈上,然后让我的应用程序服务器环境加载并处理调用。 调用完成后,我执行 env pop,并在 var 中获取 http 调用的结果(例如,通过输出缓冲区)。 我可以在同一台服务器上运行这两个服务。 有没有人有任何想法或图书馆已经这样做了?
I have an n-tier system where a frontend templating layer makes calls out to a backend application server. For instance, I need to retrieve some search results, and the frontend calls the backend to get the results.
Both the templating engine and the appserver are written in PHP. I currently use PHPed to initiate debug sessions to the templating engine, however, when the http request goes out to remote service my debugger just sits and waits for the IO to complete.
What I would like to do is emulate the HTTP call but really just stay inside my PHP process, do a giant push of the environment onto some kind of stack, then have my appserver environment load and process the call. After the call is done, I do an env pop, and get the results of the http call in a var (for instance, via an output buffer). I can run both services on the same server. Does anyone have any ideas or libraries that already do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您不能运行调试器并在应用程序服务器中设置断点吗? 两种不同的调试会话 - 一种用于捕获模板引擎调用,另一种用于捕获应用程序服务器中的调用。
您应该能够在模板引擎调试会话中跟踪应用程序服务器的输出。
如果无法运行两个调试会话,则通过捕获模板引擎的输出来为应用程序服务器创建一些测试输入,并将单个调试器与测试应用程序服务器输入一起使用。
Can you not run a debugger and set a breakpoint in the appserver too? Two different debug sessions - one to trap the templating engine call and one to trap the call in the appserver.
You should be able to trace the output from the appserver in the templating engine debugging session.
If it is not possible to run two debug sessions then create some test inputs for the appserver by capturing outputs from the templating engine and use a single debugger with your test appserver inputs.
这是令人尴尬的粗糙,并且完全没有对调试器如何工作的任何研究,但是您是否尝试过
在入口点添加到您调用的例程? (假设两个进程在同一台机器上运行)。
我已使用此技术重新进入通过 AMFPHP 调用的进程。 我有一个 PHP 文件将 Flash 文件加载到浏览器中,然后使用 AMFPHP 回调 PHP,所有这些都在同一服务器上。 当我点击 debugBreak() 行时,PhpED 重新获得控制权。
This is embarrassingly crude, and quite free of any study of how the debugger works, but have you tried adding
at the entry points to your called routine? (Assuming both processes running on the same machine).
I have used this technique to break back into a process called via AMFPHP. I have had a PHP file loading Flash file into browser, which then calls back to PHP using AMFPHP, all on the same server. When I hit the debugBreak() line, PhpED regains control.
为什么不使用 HTTP 嗅探器? 类似于 tcpflow 之类的东西。
或者,您可以将每个请求的完整 XML 记录到文件中。 回复。
不幸的是,从你的问题中并不清楚你想要实现什么,所以这些只是猜测。 您可能应该更清楚地说明您要解决的问题。
您可以重构调用删除服务的代码并使用依赖项注入和模拟。 这将允许您通过提供“模拟”但有效的数据来隔离前端和后端的开发。
希望有帮助。
Why don't you use an HTTP sniffer? Something like tcpflow.
Alternatively, you could just log the complete XML to a file for each request & response.
Unfortunately it's not clear from your question what you're trying to achieve so these are just guesses. You should probably state more clearly exactly what problem you're trying to solve.
You could possibly re-factor your code that calls out to the remove service and use dependency injection and mocks. That would allow you to isolate the development of the front-end with the back by suppling "mocked" but valid data.
Hope that helps.
我可以假设您正在谈论 PHP 中缺少线程,因此服务会停止程序流并停止调试器吗? 解决这个问题的方法有很多,但它们都很困难、麻烦而且很老套。
例如,如果您使用像 Zend 这样的框架来处理 HTTP 流量,您可以破解 HTTP 类以使用原始套接字来进行服务读/写而不是内置的东西,并创建一个小型任务切换器(循环:)跟踪正在发生的事情。
当然,您也可以使用 fopen ( 'http://...' ) 并在循环中分块读取,这可以解决问题,但您需要打开流中的 http: 支持。
Can I assume you're talking about the lack of threads in PHP, so the service stops the flow of your program and halts the debugger? There's ways around it, but they are hard, cumbersome and hackish.
For example, if you use a framework like Zend for the HTTP traffic, you can hack the HTTP class to use primitive sockets for the service reading/writing instead of the built-in stuff, and create a small task switcher (loop :) to track what's going on.
You could of course use fopen ( 'http://...' ) and fread in chunks in a loop as well, that could do the trick, but you need http: support in streams turned on.
我对 PHP 调试知之甚少,并且我不确定我是否遵循“将环境推送到某种堆栈上”,但我想知道 netcat + 一些 shell 脚本在这里是否有用用于故障排除?
您可以使用 netcat 来:
http://www.plenz.com/netcat-tips
您可以使用它在一端删除假冒的网络服务:
...并且您当然可以使用它监听端口可以非常清楚地看到 Web 服务的传入请求是什么样的。
您能否使用一个以 netcat 作为中间人的 shell 脚本,其作用类似于您的 Web 服务,立即返回一些通用内容以使您的 PHP 满意,然后将请求传递到您实际的应用程序服务器并记录结果?
超级简单。
netcat 网络服务器 http://img240.imageshack.us/img240/791/netcat.jpg< /a>
I don't know much about PHP debugging, and I'm not sure I follow 'push of the environment onto some kind of stack', but I wonder if netcat + some shell scripting could be useful here for troubleshooting ?
You can use netcat to:
http://www.plenz.com/netcat-tips
You could use it to stub out a fake webservice on the one end:
... and you can certainly use it listening on a port to very clearly see what the incoming requests to the webservice look like.
Could you use a shell script with netcat as middle man that acts like your webservice, immediately returns something generic to make your PHP happy, then passes the request on to your actual appserver and logs the results?
Super simple.
netcat webserver http://img240.imageshack.us/img240/791/netcat.jpg
这不是开源的,但请查看 Charles。 它作为代理工作,是迄今为止我见过的最好的调试代理。 它适用于 linux、os/x 和 windows。
几乎任何 HTTP 库都允许您指定代理。
This is not open source, but check out Charles. It works as a proxy, and is the best debugging proxy I've seen to date. It works on linux, os/x and windows.
Pretty much any HTTP library will allow you to specify a proxy.
PhpED 支持并行调试会话 - 这意味着您可以开始调试向初始服务器发出请求以及服务器间请求的代码。 您所需要做的就是在相应的项目中设置断点并在服务器之间传递调试器请求。 通常,您可以通过将 DBGSESSID(调试器请求)变量的值及其值重新传输到第二个服务器来完成此操作。 该变量可以在 $_COOKIES 和/或 $_GET 中找到(取决于您如何开始调试——从 IDE 或使用调试器工具栏)。 要将变量重新传输到辅助服务器,您可以将其添加到 POST 变量或作为 URL 参数或 cookie。 如果您的服务器从 get/post/cookies 中过滤掉所有内容,则您无法做到这一点,请尝试嵌入 DebugBreak() 调用。
确保您的所有服务器都可以通过请求中的 IP 地址找到 IDE,并允许连接回 IDE——例如,您在防火墙和 LinuxSE 中有必要的规则(购买默认值,现在所有现代 Linux 中都启用了此 SE 层) )。 我花了一天时间才弄清楚为什么我的服务器无法连接。
如果无法从服务器连接到 IDE(如果带有 IDE 的工作站位于不同的网络中,例如在您家里),您可以使用 ssh 隧道。 当然,在本例中,IDE 地址是 localhost。
PhpED supports parallel debug sessions - meaning you can start debugging code that issues request to initial server and then inter-server requests too. All you need is to set breakpoints in corresponding projects and pass debugger request between the servers. Normally you can do this by re-transmitting value of DBGSESSID (the debugger request) variable with its value to the 2nd server. The variable can be found among $_COOKIES and/or $_GET (depending on how you start debugging -- from the IDE or usign Debugger Toolbar). To re-transmit the variable to the secondary server(s) you can add it to POST variables or as URL parameter or cookie. If you can't do that f.e. if your server filters out all from get/post/cookies, try to embed DebugBreak() call.
Make sure that all your servers can find the IDE by its IP address in the request and allowed to connect back to the IDE -- e.g. you have necessary rules in firewall and LinuxSE (buy default this SE layer is enabled in all modern Linuxes these days). It took me a day to figure out why my server can't connect.
In case if connection from the server to the IDE is not possible (if workstation with the IDE is in a different network, for example at your home), you can use ssh tunnels. In this case the IDE address is localhost, of course.