How do I render javascript from another site, inside a PHP application?

后端 未结 3 1346
忘了有多久
忘了有多久 2021-01-23 05:46

What I\'m trying to do is read a specific line from a webpage from inside of my PHP application. This is my experimental setup thus far:

      

        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-23 06:08

    You can get the entire web page as a file like this:

    function get_data($url)
    {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }
    $returned_content = get_data('http://example.com/page.htm');
    $my_file = 'file.htm';
    $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
    fwrite($handle, $returned_content);
    

    Then I suppose you can use a class such as explained in this link below as a guide to separate the javascript from the html (its in the head tags usually). for linked(imported) .js files you would have to repeat the function for those urls, and also for linked/imported css. You can also grab images if you need to save them as files. http://www.digeratimarketing.co.uk/2008/12/16/curl-page-scraping-script/

提交回复
热议问题