“Run” HTMLUnit with PHP

只愿长相守 提交于 2019-12-08 05:06:21

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7211075/run-htmlunit-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!