首页 > 解决方案 > 我需要将变量传递给从 php exec 命令调用的 javascript

问题描述

我有一个通过 exec 命令调用 phantomjs 的 php 文件。phantom.js 文件调用需要包含变量的 url。我需要将这些变量与 exec 命令一起从 php 发送,并让变量显示在 phantom.js url 中。

这是我当前的硬编码php:

$response = [];
exec('/usr/bin/phantomjs phantomjstest.js', $response);
$data = $response[19];

和 phantom.js

var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent111';
page.open('https://www.aa.com/travelInformation/flights/status/detail?search=AA|1698|2019,1,23&ref=search', function(status) {
if (status !== 'success') {
    console.log('Unable to access network');
} else {
    var ua = page.evaluate(function() {
        return document.getElementById('aa-content-frame').innerHTML;
    });
    console.log(ua);
}
phantom.exit();
});

我想做的是更改 php 以便它将 2 个变量(来自表单提交)传递给 javascript。就像是:

PHP:

exec('/usr/bin/phantomjs phantomjstest.js?variable1=$forminput1&variable2=$forminput2', $response);

JS

page.open('https://www.aa.com/travelInformation/flights/status/detail?search=AA|variable1|variable2&ref=search', function(status) {

或者我可以在 php 中构建整个 URL 并将其与 exec 命令一起发送。

任何一种方法的想法,或从这里到那里的其他方式,都非常感谢。谢谢。


根据评论中的建议,我更新了我的 PHP 以包含:

exec("/usr/bin/phantomjs phantomjstest.js https://www.aa.com/travelInformation/flights/status/detail?search=AA|1698|2019,1,23&ref=search", $response);

和我的 JS:

var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent111';

var system = require('system');
var args = require('system').args;
var address = system.args[0];
page.open(address, function (status) {

if (status !== 'success') {
    console.log('Unable to access network');
} else {
    var ua = page.evaluate(function() {
        return document.getElementById('aa-content-frame').innerHTML;
    });
    console.log(ua);
}
phantom.exit();
});

但我得到的只是一个空响应。知道为什么地址没有通过 PHP exec 传递或没有被 JS 拾取和运行吗?

谢谢。

****** 解决方案 ********

有几件事需要修复,我要感谢那些在下面发表评论的人,以及在 SO 上为类似问题提供解决方案的其他人。

首先,JS 不喜欢 URL 中的 & 符号。我不得不在传递的参数中使用 @ ,然后在 JS 端替换它。

其次,JS 不喜欢 URL 中的管道符号,所以我不得不转义它们。

完成的 PHP 看起来像:

exec("/usr/bin/phantomjs phantomjstest.js https://www.aa.com/travelInformation/flights/status/detail?search=AA\|$flight\|$date@ref=search", $response);

和JS喜欢:

var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent111';

var system = require('system');
var args = require('system').args;
var address = system.args[1];
var address = address.replace(/@/gi,"&");
page.open(address, function (status) {

if (status !== 'success') {
    console.log('Unable to access network');
} else {
    var ua = page.evaluate(function() {
        return document.getElementById('aa-content-frame').innerHTML;
    });
    console.log(ua);
}
phantom.exit();
});

再次感谢大家!!!!

标签: javascriptphp

解决方案


****** 解决方案 ********

有几件事需要修复,我要感谢那些在下面发表评论的人,以及在 SO 上为类似问题提供解决方案的其他人。

首先,JS 不喜欢 URL 中的 & 符号。我不得不在传递的参数中使用 @ ,然后在 JS 端替换它。

其次,JS 不喜欢 URL 中的管道符号,所以我不得不转义它们。

完成的 PHP 看起来像:

exec("/usr/bin/phantomjs phantomjstest.js 
https://www.aa.com/travelInformation/flights/status/detail? 
search=AA\|$flight\|$date@ref=search", $response);

和JS喜欢:

var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent111';

var system = require('system');
var args = require('system').args;
var address = system.args[1];
var address = address.replace(/@/gi,"&");
page.open(address, function (status) {

if (status !== 'success') {
    console.log('Unable to access network');
} else {
var ua = page.evaluate(function() {
    return document.getElementById('aa-content-frame').innerHTML;
});
console.log(ua);
}
phantom.exit();
});

推荐阅读