在 PHP 中通过 exec 发送后无法解码 json 数组
Unable to decode json array after sending it via exec in PHP
我有一个 array
看起来像这样:
Daemon.php
$data = array(
'notificationId' => $notificationId,
'userId' => $userId,
'appId' => $appId,
'message' => $message,
'destinationUrl' => $destinationUrl,
'method' => $method,
'notificationTime' => $notificationTime,
'timeReceived' => $timeReceived,
'impressions' => $impressions,
'clicks' => $clicks,
'numberOfUsers' => $numberOfUsers,
'campaignId' => $campaignId,
'targetGroups' => $targetGroups,
'notificationType' => $notificationType,
'status' => $status,
'appGroup' => $appGroup
);
我正在通过 exec
发送,如下所示:
$data=json_encode($data);
exec("php path/where/script/is/useArray.php ".$data." &");
并尝试像在其他脚本中那样使用它:
useArray.php
$logData=$argv[1];
json_decode($logData);
为了查看在 useArray.php
上收到了哪些数据,我将这个 $logData
数组放入服务器上的 txt 文件中,如下所示:
file_put_contents(__DIR__ .'/log/testiranje.txt', print_r($logData,true)."\n", FILE_APPEND);
但是发送json
好像解码不对。这就是 $logData
在 testiranje.txt
:
中的样子
{notificationId:478,userId:92,appId:1512823699024883,message:joj,destinationUrl:https:\/\/www.servis-racunara.net\/pages\/,method:2}
所以这是我在 json_decode
之后得到的一些奇怪的 json 格式。当然我不知道如何使用这种格式,因为我不能做类似的事情:
$notificationId = $logData['notificationId'];
您正在通过 shell 语法解释字符串,该语法有自己非常庞大且复杂的一组 特殊字符 。一方面,"
引号由 shell 解释,因此从结果值中删除。
如果你想通过 shell 传输任意字符串(或者实际上通过任何具有自己的语法和特殊字符的中间语言),你需要 escape 恰如其分:
exec("php path/where/script/is/useArray.php " . escapeshellarg($data) . " &");
见http://php.net/escapeshellarg。
话虽如此,我会避免这种调用并使用其他通信机制,例如使用 ØMQ、Gearman 等的 queue/worker 设置。但这超出了本主题的范围。
您通常不能在 shell 中键入随机字符并让它们作为常规文本通过,这就是 escapeshellarg() 存在的原因(尽管根据我的经验,它只能在 Unix shells 并且经常在 Windows) 上严重失败。
无论如何,命令行参数只适用于小参数。如果你需要传输复杂的数据,你最好使用其他机制:
- 标准输入
- 临时文件
对于前者,您必须转储 exec()
并使用例如proc_open()
—您可以在 manual page.
中找到使用示例
对于后者,请在file system functions中选择你最喜欢的。对于小文件,file_put_contents()
/ file_get_contents()
组合可能就好了。
我有一个 array
看起来像这样:
Daemon.php
$data = array(
'notificationId' => $notificationId,
'userId' => $userId,
'appId' => $appId,
'message' => $message,
'destinationUrl' => $destinationUrl,
'method' => $method,
'notificationTime' => $notificationTime,
'timeReceived' => $timeReceived,
'impressions' => $impressions,
'clicks' => $clicks,
'numberOfUsers' => $numberOfUsers,
'campaignId' => $campaignId,
'targetGroups' => $targetGroups,
'notificationType' => $notificationType,
'status' => $status,
'appGroup' => $appGroup
);
我正在通过 exec
发送,如下所示:
$data=json_encode($data);
exec("php path/where/script/is/useArray.php ".$data." &");
并尝试像在其他脚本中那样使用它:
useArray.php
$logData=$argv[1];
json_decode($logData);
为了查看在 useArray.php
上收到了哪些数据,我将这个 $logData
数组放入服务器上的 txt 文件中,如下所示:
file_put_contents(__DIR__ .'/log/testiranje.txt', print_r($logData,true)."\n", FILE_APPEND);
但是发送json
好像解码不对。这就是 $logData
在 testiranje.txt
:
{notificationId:478,userId:92,appId:1512823699024883,message:joj,destinationUrl:https:\/\/www.servis-racunara.net\/pages\/,method:2}
所以这是我在 json_decode
之后得到的一些奇怪的 json 格式。当然我不知道如何使用这种格式,因为我不能做类似的事情:
$notificationId = $logData['notificationId'];
您正在通过 shell 语法解释字符串,该语法有自己非常庞大且复杂的一组 特殊字符 。一方面,"
引号由 shell 解释,因此从结果值中删除。
如果你想通过 shell 传输任意字符串(或者实际上通过任何具有自己的语法和特殊字符的中间语言),你需要 escape 恰如其分:
exec("php path/where/script/is/useArray.php " . escapeshellarg($data) . " &");
见http://php.net/escapeshellarg。
话虽如此,我会避免这种调用并使用其他通信机制,例如使用 ØMQ、Gearman 等的 queue/worker 设置。但这超出了本主题的范围。
您通常不能在 shell 中键入随机字符并让它们作为常规文本通过,这就是 escapeshellarg() 存在的原因(尽管根据我的经验,它只能在 Unix shells 并且经常在 Windows) 上严重失败。
无论如何,命令行参数只适用于小参数。如果你需要传输复杂的数据,你最好使用其他机制:
- 标准输入
- 临时文件
对于前者,您必须转储 exec()
并使用例如proc_open()
—您可以在 manual page.
对于后者,请在file system functions中选择你最喜欢的。对于小文件,file_put_contents()
/ file_get_contents()
组合可能就好了。