Get the content (text) of an URL after Javascript has run with PHP

后端 未结 4 1726
故里飘歌
故里飘歌 2020-11-29 09:08

Is it possible to get the content of a URL with PHP (using some sort of function like file_get_contents or header) but only after the execution of

4条回答
  •  天涯浪人
    2020-11-29 09:56

    Update 2 Adds more details on how to use phantomjs from PHP.

    Update 1 (after clarification that javascript on target page need to run first)

    Method 1:Use phantomjs(will execute javascript);

    1. Download phantomjs and place the executable in a path that your PHP binary can reach.

    2. Place the following 2 files in the same directory:

    get-website.php

    
    

    get-website.js

    var webPage = require('webpage');
    var page = webPage.create();
    
    page.open('http://google.com/', function(status) {
     console.log(page.content);
      phantom.exit();
    });
    

    3. Browse to get-website.php and the target site, http://google.com contents will return after executing inline javascript. You can also call this from a command line using php /path/to/get-website.php.

    Method 2:Use Ajax with PHP (No phantomjs so won't run javascript);

    /get-website.php

    
    

    test.html

    
    
    
    
    on demo
    
    
    
    
    
    
    
    
    
    

提交回复
热议问题