【发布时间】:2018-01-19 10:54:19
【问题描述】:
我正在尝试在我的 php 脚本上执行此命令。 shell_exec("phantomjs C:\sample\sample.js");
但它在几秒钟后返回 null。我预计它应该需要很长时间,因为我已经在 cmd 上测试了这个命令并且它正在工作。
我正在使用 ajax 调用执行此脚本的控制器,以便它可以在后台运行。问题是它调用 exec / shell_exec 命令的 php 端。 谁能赐教?
这是我的代码:
View.php
<a href="#" class="text-dark launch" id="<?php echo $row->id; ?>"> Launch</a>
ajax.js
$('.launch').on('click', function () {
$.ajax({
type: 'POST',
url: 'http://localhost/sample/run_scrape',
success: function (status) {
console.log(status);
}
});
});
sample.php
public function run() {
$html = shell_exec("phantomjs C:\sample\scraper.js");
echo json_encode($html);
}
scraper.js
var page = require('webpage').create();
page.open('https://detectmybrowser.com/', function(status){
console.log("Status: " + status);
if(status === "success"){
page.render('example.png');
}
phantom.exit();
});
【问题讨论】:
-
phantomjs在您的命令行中可用吗?它在您的系统路径中吗?