我一直在尝试将我的phantomjs控制台输出传递回执行它的php文件,但我只得到这个代码的空数组。它也会立即发生,这让我觉得它试图在phantomjs脚本运行之前获得返回。
<?php
$user= 'dafjdh@kjsdf.com';
$pass='dumfmh';
$response = exec("/home/jef28/public_html/swipr/phantomjs /home/jef28/public_html/swipr/login.js $user $pass", $output);
echo $output;
?>
我的.js文件的最后一行是
console.log(document.querySelectorAll('html')[0].outerHTML);
return document.querySelectorAll('html')[0].outerHTML;
如何将控制台输出传递回php?
答案 0 :(得分:2)
我使用此功能,效果很好。
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;
}