从PHP执行PhantomJS

问题描述 投票:0回答:1

我正在尝试在我的php脚本上执行此命令。 shell_exec(“phantomjs C:\ sample \ sample.js”);

但只需几秒钟它就会返回null。我期待它应该需要很长时间,因为我已经在cmd上测试了这个命令并且它正在工作。

我正在使用ajax来调用执行此脚本的控制器,以便它可以在后台运行。问题是php端调用exec / shell_exec命令。任何人都可以开导我吗?

这是我的代码:

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();
});
php ajax phantomjs
1个回答
1
投票

使用phantomjs的绝对路径为我工作

$html = shell_exec("C:\your_path_to_phantomjs_bin\phantomjs C:\sample\scraper.js");
© www.soinside.com 2019 - 2024. All rights reserved.