“运行” HTMLUnit 与 PHP
所以我在 CentOS 服务器上安装了 Java。我现在希望能够使用 PHP 运行 HTMLUnit 来获取完全渲染的网页,然后将结果返回给用户。
我在 HTMLUnit 上看到了“简单”的示例,但我对 Java 几乎一无所知,并且不知道需要去哪里或运行它才能使测试用例正常工作(即获取 Google 的主页)。
public void getURL() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://google.com"); // Pass in URL
// RETURN "page"
}
一旦测试开始工作,我就需要能够“传递”所需的 URL,然后“捕获”输出。
到目前为止,谷歌搜索让我在原地打转。有谁有一个简单示例的链接,以及如何将其与 PHP 集成的指导吗?
谢谢!
So I have installed Java on my CentOS server. I now want to be able to use PHP to run HTMLUnit to get a fully rendered webpage and then return the results to the user.
I see the "simple" example on HTMLUnit but I know next to nothing about Java and don't know where that needs to go or be ran to even get the test case working (i.e. getting Google's homepage).
public void getURL() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://google.com"); // Pass in URL
// RETURN "page"
}
Once the test is working I would need to be able to "pass" in the desired URL and then "capture" the output.
So far Googling as me running in circles. Does anyone have a link to a simple example, and then pointers on how to integrate it with PHP?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 PHP 的 shell_exec() 调用来启动 HTMLunit 控制台行并捕获输出。至于代码,这应该可以帮助您开始:
然后,从 php:
我目前无法测试它,对于任何代码错误,我们深表歉意。
You can use PHP's shell_exec() call to start HTMLunit console line and capture the output. As for the code, this should get you started:
Then, from php:
I can't test it at the moment, so sorry for any code mistakes.
使用 java 获取 HTML
从 php 获取结果
$output 是您期望的数据。
Get HTML using java
Get result from php
$output is your expected data.