user3745602
user3745602

Reputation: 315

Pass Phantomjs console to php file

I have been trying to pass my phantomjs console output back to the php file that executed it, but I only get and empty array with this code. It also happens immediately, which makes me think its attempting to get the return before the phantomjs script has even run.

<?php
$user= '[email protected]';
$pass='dumfmh';
$response = exec("/home/jef28/public_html/swipr/phantomjs /home/jef28/public_html/swipr/login.js $user $pass", $output);
echo $output;
?>

The last lines of my .js file are

console.log(document.querySelectorAll('html')[0].outerHTML);
      return document.querySelectorAll('html')[0].outerHTML;

How can I pass the console output back to php?

Upvotes: 2

Views: 1158

Answers (1)

Amir K
Amir K

Reputation: 36

I use this function and it works just fine .

function execute($script, $args = array(), $options = array(), $bin = 'F:/----/phantomjs-1.9.8-windows/phantomjs.exe', $debug = true) {

    $option_str = '';
    foreach ($options as $option => $value)
    {
        $option_str .= '--'.$option.'='.$value.' ';
    }

    // Escape
    $cmd = escapeshellcmd("{$bin} {$option_str}{$script} " . implode(' ', $args));
    if($debug) $cmd .= ' 2>&1';
    // Execute
    $result = shell_exec($cmd);
    if($debug) return $result;
    if($result === null) return false;

    // Return
    if(substr($result, 0, 1) !== '{') return $result; // not JSON
    $json = json_decode($result, $as_array = true);
    if($json === null) return false;
    return $json;

}

Upvotes: 2

Related Questions