Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Phantomjs-有没有办法将参数传递给_Php_Phantomjs - Fatal编程技术网

Php Phantomjs-有没有办法将参数传递给

Php Phantomjs-有没有办法将参数传递给,php,phantomjs,Php,Phantomjs,假设我用PHP编写了这段代码来调用Phantomjs shell_exec(“phantomjs a-phantomjs-file.js”) 有没有办法将数据从PHP传递到phantomjs文件?可能是某种命令行参数?此处列出了phantomjs的命令行参数列表: 您可以使用字符串连接或插值从PHP传递它们,只要注意并小心防止注入攻击(如果参数可能来自用户输入)。您可能可以这样做 $array = array("option1"=>"Test", "option2"=>"test2

假设我用PHP编写了这段代码来调用Phantomjs

shell_exec(“phantomjs a-phantomjs-file.js”)


有没有办法将数据从PHP传递到phantomjs文件?可能是某种命令行参数?

此处列出了phantomjs的命令行参数列表:


您可以使用字符串连接或插值从PHP传递它们,只要注意并小心防止注入攻击(如果参数可能来自用户输入)。

您可能可以这样做

 $array = array("option1"=>"Test", "option2"=>"test2"); // this is what you want in phantom
 $tmp = tempnam("/path/to/tempDir/", "PHANTOM_");
 file_put_contents($tmp, "var params = ".json_encode($array)."; ".file_get_contents("a-phantomjs-file.js"));
 shell_exec("phantomjs ".escapeshellarg($tmp));
 unlink($tmp);
然后在幻影文件中,您可以访问以下属性:

 params.option1

我使用PHP向PhantomJS发送和接收数据,如:

$command = '/path/to/phantomjs /path/to/my/script.js ' . escapeshellarg($uri);
$result_object = json_decode(shell_exec($command));
警告:请确保卸载用户输入,以防止其他人在您的服务器上执行代码

在javascript中,此URI变量可用作
system.args
数组的第二个元素(第一个元素是您正在调用的脚本的名称):

javascript完成后,您可以在退出PhantomJS之前输出JSON变量:

console.log(JSON.stringify({
  "status": "success"
}));
phantom.exit();
在本例中的PHP代码的第一行中,我们已经使用了
json\u decode()
将纯文本json返回值解码到一个对象中,因此我们可以从PHP中使用以下方法访问
status
变量:

print $result_object->status;

我看到了这个列表,但这里没有任何东西可以帮助我将一些数据从PHP传递到phantomjsfile@arvinsim您的意思是在执行phantomjs之前要编辑
phantomjs文件.js
文件吗?你可以用fopen打开文件,然后像在PHP中编辑文件一样对其进行编辑。我的想法是这样的:我有来自PHP的数据,就像uri一样。我想将该数据传递给将要执行的phantomjs文件。
print $result_object->status;