“Run” HTMLUnit with PHP

徘徊边缘 提交于 2019-12-06 15:49:42

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.

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.

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