简体   繁体   English

将数据从CasperJS脚本发送到PHP

[英]Send data from CasperJS script to PHP

I'm trying to get data from CasperJS like this: 我试图像这样从CasperJS获取数据:

Contents of file.php : file.php内容:

$casperjs = "casperjs";
$script = 'add_site.js';
$arg0 = $_POST['new_site'];  
$command = "$casperjs $script $arg0";
$result = shell_exec($command);
echo $result;

Contents of casperjs.js : casperjs.js内容:

casper.then(function() {
  // some code
  utils.dump(JSON.stringify(site));
});
casper.run(function() {
  this.exit();
});

I have nothing on output. 我没有任何输出。

Not 100% on CasperJS, but looking at your PHP code try using json_encode in your script like this: 在CasperJS上不是100%,但是查看您的PHP代码,尝试在脚本中使用json_encode ,如下所示:

$casperjs = "casperjs";
$script = 'add_site.js';
$arg0 = $_POST['new_site'];  
$command = "$casperjs $script $arg0";
$result = shell_exec($command);
echo json_encode($result);

Also try setting explicit JSON headers for your output like this: 也可以尝试为输出设置显式JSON标头,如下所示:

$casperjs = "casperjs";
$script = 'add_site.js';
$arg0 = $_POST['new_site'];  
$command = "$casperjs $script $arg0";
$result = shell_exec($command);
$json_data = json_encode($result);
header('X-JSON: (' . $json_data . ')');
header('Content-type: application/x-json');
echo $json_data;

I had a problem with casperjs. 我在使用casperjs时遇到了问题。

PHP must be so: PHP必须是这样的:

$casperjs = "/home/k/kreker92/casperjs/bin/casperjs";
$script = 'add_site.js';
$arg0 = $_POST['new_site'];
putenv("PHANTOMJS_EXECUTABLE=/home/k/kreker92/phantomjs/bin/phantomjs");
$command = "$casperjs $script $arg0";
$result = shell_exec($command);
$json_data = json_encode($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM