我在php上运行这个脚本:
<?
echo $result = shell_exec("phantomjs \script.js");
?>
的script.js:
var page = require("webpage").create();
page.open("http://livescore.in/", function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
window.setTimeout(function () {
console.log(page.content);
phantom.exit();
}, 5000);
}
});
php文件,phantom.js,script.js位于同一个文件夹中。
PhantomJS版本 - 2.2.1
结果,脚本无限期地加载。
答案 0 :(得分:0)
问题的原因很可能是文件路径不正确。在您的情况下,最简单的解决方案是设置phantomjs二进制和脚本的完整路径。
<?php
echo $result = shell_exec("/path/to/work/folder/phantomjs /path/to/work/folder/script.js");
?>
要查找完整路径:登录到您的服务器,导航到工作文件夹,然后发出命令
pwd
该命令的输出是需要在shell_exec
中使用的路径。
这样你就不会对路径感到困惑,脚本总会运行。但我建议您花点时间阅读Linux中的absolute/relative路径。
如果所有文件都在同一个文件夹中,这也应该有效:
<?php
echo $result = shell_exec("phantomjs script.js");
?>