How to get HTML with JavaScript support?

I would like to get HTML code from a page with PHP. So I do this:

$url = 'http://en.wikipedia.org/wiki/New_York_City';
$html = file_get_html($url);

The problem is that Wikipedia does not send the tag <script>to the PHP request, so it does not show JavaScript. I assume that since Wikipedia sees that the "requestor" does not have JavaScript, so it does not send tags <script>.

How can I tell Wikipedia that my PHP has JavaScript enabled?

I heard about the context of the stream, but I don't know how to set JavaScript for it.

+3
source share
4 answers

Thanks symcbean, here is the solution.

I added:

ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');

And now it sends a corret script block.

;)

+1
source

$url = 'http://en.wikipedia.org/wiki/New_York_City';
$html = file_get_contents($url);

PHP.

+2

It seems like the function file_get_html()removes the blocks <script>because I tried to request GET /wiki/Main_Page HTTP/1.1from Fiddler without request headers, and it returned the blocks <script>in the response.

+1
source

You can use iframe.

You can also use something like jQuery to capture a page (or specific parts of a page) into your website.

-5
source

Source: https://habr.com/ru/post/1736160/


All Articles