Javascript 如何将动态JS文件传递到PhantomJS
我试图获取am input的值,使用Javascript 如何将动态JS文件传递到PhantomJS,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我试图获取am input的值,使用AJax将这些变量提交到php函数中,使用从AJax传递的这些参数从所述php函数调用PhantomJS,并将结果返回到HTML页面。我正在将变量传递到PHP文件,这很好,问题是使用脚本和三个参数调用PhantomJS 这是我的PHP页面上调用PhantomJS echo json_encode(array("abc" => shell_exec('/Applications/XAMPP/htdocs/scripts/phantom/bin/phanto
AJax
将这些变量提交到php函数中,使用从AJax
传递的这些参数从所述php函数调用PhantomJS,并将结果返回到HTML
页面。我正在将变量传递到PHP
文件,这很好,问题是使用脚本和三个参数调用PhantomJS
这是我的PHP
页面上调用PhantomJS
echo json_encode(array("abc" => shell_exec('/Applications/XAMPP/htdocs/scripts/phantom/bin/phantomjs /Applications/XAMPP/htdocs/scripts/phantom/examples/test.js 2>&1',$website)));
这是shell脚本中引用的脚本:
var args = require('system').args;
args.forEach(function(arg, i) {
console.log(i+'::'+arg);
});
var page = require('webpage').create();
var address = args[1];
page.open(address, function () {
console.log("Done")
});
正如您所看到的,这应该是一个相对简单的过程,除了没有任何东西被echo
'd。每个文件的权限都非常充分,我确信这些文件正在执行,因为如果我更改shell脚本以完美地运行所有echo
和日志
另请注意此脚本正在我的web服务器上执行,因此我不能100%确定是否存在系统
变量
有什么想法吗?第一期,shell_exec()接受一个参数()。但是,您的示例将shell参数($website)作为shell_exec()上的第二个参数传递
更正的示例:
$shellReturn = shell_exec("/Applications/XAMPP/htdocs/scripts/phantom/bin/phantomjs /Applications/XAMPP/htdocs/scripts/phantom/examples/test.js " . $website);
echo json_encode(array("abc" => $shellReturn));
为了简单起见,我排除了错误管道的重定向。此外,我建议您将参数作为用base64编码包装的JSON传递。这消除了导致多个参数的URL间距。一旦PhantomJS接收到系统参数,使用atob()返回JSON并迭代JSON obj,而不是原始字符串参数
我还要向您指出这个项目:,在引擎盖下是PhantomJS的一个实例,该项目只是包装了PhantomJS的功能
$myUrl = "http://www.example.com"; //replace with content of your $website variable
$windowObj = \MTS\Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl);
//if you want the DOM or maybe screenshot and any point run:
$dom = $windowObj->getDom();
$imageData = $windowObj->screenshot();