【问题标题】:Execute PhantomJS from PHP从 PHP 执行 PhantomJS
【发布时间】: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 在您的命令行中可用吗?它在您的系统路径中吗?

标签: php ajax phantomjs


【解决方案1】:

对 phantomjs 使用绝对路径对我有用

$html = shell_exec("C:\your_path_to_phantomjs_bin\phantomjs C:\sample\scraper.js");

【讨论】:

    猜你喜欢
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 2015-06-17
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    相关资源
    最近更新 更多