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

前端 未结 3 1540
梦谈多话
梦谈多话 2020-12-11 03:04

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

相关标签:
3条回答
  • 2020-12-11 03:32

    There is one quite tricky way to achieve it using php. If you' really like it to work for php you could potentially use Codeception setup in junction with Selenium and use Chrome browser webdriver in headless mode.

    Here are some general steps to have it working.

    1. You make sure you have codeception in your PHP project https://codeception.com

    2. Download chrome webdriver: https://chromedriver.chromium.org/downloads

    3. Download selenium: https://www.seleniumhq.org/download/

    4. Configure it accordingly looking into documentation of codeception framework.

    5. Write codeception test where you can use expression like $I->wait(5) for waiting 5 seconds or $I->waitForJs('js expression here') for waiting for js script to complete on the page.

    6. Run written in previous step test using command php vendor/bin/codecept path/to/test

    0 讨论(0)
  • 2020-12-11 03:36

    Not knowing a lot about the page you are retrieving or the calculations you want to include, but it could be an option to cURL straight to the URL serving those ajax requests. Use something like Firebug to inspect the Ajax calls being made on your target page and you can figure out the URL and any parameters passed. If you do need the full web page, maybe you can cURL both the web page and the Ajax URL and combine the two in your PHP code, but then it starts to get messy.

    0 讨论(0)
  • 2020-12-11 03:46

    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).

    0 讨论(0)
提交回复
热议问题