Is there a way to let cURL wait until the page's dynamic updates are done?

user1467267 picture user1467267 · Jan 31, 2013 · Viewed 13.5k times · Source

I'm fetching pages with cURL in PHP. Everything works fine, but I'm fetching some parts of the page that are calculated with JavaScript a fraction after the page is loaded. cURL already send the page's source back to my PHP script before the JavaScript calculations are done, thus resulting in wrong end-results. The calculations on the site are fetched by AJAX, so I can't reproduce that calculation in an easy way. Also I have no access to the target-page's code, so I can't tweak that target-page to fit my (cURL) fetching needs.

Is there any way I can tell cURL to wait until all dynamic traffic is finished? It might be tricky, due to some JavaScripts that are keep sending data back to another domain that might result in long hangs. But at least I can test then if I at least get the correct results back.

My Developer toolbar in Safari indicates the page is done in about 1.57s. Maybe I can tell cURL statically to wait for 2 seconds too?

I wonder what the possibilities are :)

Answer

Jan Hančič picture Jan Hančič · Jan 31, 2013

cURL does not execute any JavaScript or download any files referenced in the document. So cURL is not the solution for your problem.

You'll have to use a browser on the server side, tell it to load the page, wait for X seconds and then ask it to give you the HTML.

Look at: http://phantomjs.org/ (you'll need to use node.js, I'm not aware of any PHP solutions).