Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL PHP - load a fully page

Tags:

php

curl

I am currently trying to load an HTML page via cURL. I can retrieve the HTML content, but part is loaded later via scripting (AJAX POST). I can not recover the HTML part (this is a table).

Is it possible to load a page entirely?

Thank you for your answers

like image 733
Florent Guenebeaud Avatar asked Oct 19 '22 04:10

Florent Guenebeaud


1 Answers

No, you cannot do this.

CURL does nothing more than download a file from a URL -- it doesn't care whether it's HTML, Javascript, and image, a spreadsheet, or any other arbitrary data; it just downloads. It doesn't run anything or parse anything or display anything, it just downloads.

You are asking for something more than that. You need to download, parse the result as HTML, then run some Javascript that downloads something else, then run more Javascript that parses that result into more HTML and inserts it into the original HTML.

What you're basically looking for is a full-blown web browser, not CURL.

Since your goal involves "running some Javascript code", it should be fairly clear that it is not acheivable without having a Javascript interpreter available. This means that it is obviously not going to work inside of a PHP program (*). You're going to need to move beyond PHP. You're going to need a browser.

The solution I'd suggest is to use a very specialised browser called PhantomJS. This is actually a full Webkit browser, but without a user interface. It's specifically designed for automated testing of websites and other similar tasks. Your requirement fits it pretty well: write a script to get PhantomJS to open your URL, wait for the table to finish rendering, and grab the finished HTML code.

You'll need to install PhantomJS on your server, and then use a library like this one to control it from your PHP code.

I hope that helps.

(*) yes, I'm aware of the PHP extension that provides a JS interpreter inside of PHP, and it would provide a way to solve the problem, but it's experimental, unfinished, would be still difficult to implement as a solution, and I don't think it's a particularly good idea anyway, so let's not consider it for the purposes of this answer.

like image 198
Simba Avatar answered Oct 21 '22 22:10

Simba