php - Get the code of a page generated with JavaScript

363

I'm trying to get the code of an URL page after it JavaScript executed.
For the exemple :

<?php
    function getVideoURL($link){
        $data = file_get_contents($link);
        print($data);
    }

    $link = htmlentities($_POST["link"]);

    getVideoURL($link);
?>

But this code give me the code of the url before javasript execution, is there a way to get it after javascript execution ?

246

Answer

Solution:

file_get_contents get the original code of the page, it doesn't execute any client side code ( javascript )

To execute javascript you will need an entire browser emulation engine -- a headless browser.

http://jonnnnyw.github.io/php-phantomjs/

use JonnyW\PhantomJs\Client;

function getVideoURL($link){
    $client = Client::getInstance();

    $request = $client->getMessageFactory()->createRequest($link, 'GET');

    $response = $client->getMessageFactory()->createResponse();

    $client->send($request, $response);

    if($response->getStatus() === 200) {

        // Dump the requested page content
        return $response->getContent();
    } else {
        return false;
    }

}

$link = htmlentities($_POST["link"]);

getVideoURL($link);

People are also looking for solutions to the problem: php - Uploading files without form?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.