2015-04-20 157 views
-1

我想运行php脚本并将参数传递给它。参数在数组中。将数组传递给php脚本

我json_encode数组:

$bot = array(
    'timing' => '* * * * *', 
    'path' => '/sompath/' 
); 

$bot = json_encode($bot); 

并将其发送给脚本:

exec('nohup php script.php "' . $bot . '" > /bot.log 2>&1 &'); 

但随后,在剧本,我不接收参数:

print_r($argv[1]); // {path:/sompath/,frequency:* 

看起来特别像星号这样的符号不会被解析并破坏JSON。

如何将数组传递给另一个脚本并确保所有参数都保留?

+0

为什么您使用json_encode函数两次? – KarthzDIGI

+0

对不起,例如错误 –

回答

2

如果你想通过JSON数据作为一个字符串,你应该先逃跑吧:

$bot = array(
    'timing' => '* * * * *', 
    'path' => '/sompath/' 
); 

$bot = json_encode($bot); 
$bot=escapeshellarg($bot); 

,然后在接收脚本:

print_r(json_decode($argv[1])); 
0

这很简单。应该使用urlencode正确传递json。

exec('nohup php script.php "' . urlencode($bot) . '" > /bot.log 2>&1 &'); 

,然后在运行script.php:

$params = urldecode($argv[1]); 
$params = json_decode($params, true);