“运行” HTMLUnit 与 PHP

发布于 2024-12-02 01:22:09 字数 507 浏览 4 评论 0原文

所以我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烈酒灼喉 2024-12-09 01:22:09

您可以使用 PHP 的 shell_exec() 调用来启动 HTMLunit 控制台行并捕获输出。至于代码,这应该可以帮助您开始:

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.lang.String;

import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;

public class myClient {
    public static void main(String[] args) throws Exception {
        // Create and initialize WebClient object
        WebClient webClient = new WebClient();
        HtmlPage page = webClient.getPage("http://google.com"); // Pass in URL
        Console.out.println(page.toString());
    }   
}

然后,从 php:

$html = shell_exec('/bin/javac myClient.java');

我目前无法测试它,对于任何代码错误,我们深表歉意。

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:

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.lang.String;

import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;

public class myClient {
    public static void main(String[] args) throws Exception {
        // Create and initialize WebClient object
        WebClient webClient = new WebClient();
        HtmlPage page = webClient.getPage("http://google.com"); // Pass in URL
        Console.out.println(page.toString());
    }   
}

Then, from php:

$html = shell_exec('/bin/javac myClient.java');

I can't test it at the moment, so sorry for any code mistakes.

谁许谁一生繁华 2024-12-09 01:22:09

使用 java 获取 HTML

    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.net.URL;
    import java.util.List;
    import java.lang.String;

    import com.gargoylesoftware.htmlunit.Page;
    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;

    public class GetHtml {

        public static void main(String[] args) throws IOException {
            WebClient webClient = new WebClient();
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setJavaScriptEnabled(false);
            HtmlPage page = webClient.getPage("http://google.com"); // Pass in URL
            String originalHtml = page.getWebResponse().getContentAsString();
            System.out.println(originalHtml);
        }

    }

从 php 获取结果

    exec("java -jar ", $output);

$output 是您期望的数据。

Get HTML using java

    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.net.URL;
    import java.util.List;
    import java.lang.String;

    import com.gargoylesoftware.htmlunit.Page;
    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;

    public class GetHtml {

        public static void main(String[] args) throws IOException {
            WebClient webClient = new WebClient();
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setJavaScriptEnabled(false);
            HtmlPage page = webClient.getPage("http://google.com"); // Pass in URL
            String originalHtml = page.getWebResponse().getContentAsString();
            System.out.println(originalHtml);
        }

    }

Get result from php

    exec("java -jar ", $output);

$output is your expected data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文