Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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中通过exec发送json数组后,无法对其进行解码_Php_Arrays_Json_Exec - Fatal编程技术网

在PHP中通过exec发送json数组后,无法对其进行解码

在PHP中通过exec发送json数组后,无法对其进行解码,php,arrays,json,exec,Php,Arrays,Json,Exec,我有一个数组,看起来像这样: Daemon.php $data = array( 'notificationId' => $notificationId, 'userId' => $userId, 'appId' => $appId, 'message' => $message, 'destinationUrl' => $destinationUrl, 'method' => $method, 'notificati

我有一个
数组
,看起来像这样:

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

);
$logData=$argv[1];
json_decode($logData);
我通过
exec
发送的,如下所示:

$data=json_encode($data);
exec("php path/where/script/is/useArray.php ".$data." &");
并尝试在其他脚本中使用它:

useArray.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

);
$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\u decode
之后得到的一种奇怪的类似于json的格式。当然,我不知道如何使用这种格式,因为我不能做任何类似于:

$notificationId   = $logData['notificationId'];

您正在通过shell语法解释字符串,它有自己非常大且复杂的特殊字符集。例如,
引号由shell解释,因此从结果值中去掉

如果您想通过shell(或实际上通过任何具有自己语法和特殊字符的中间语言)传输任意字符串,则需要适当地转义它:

exec("php path/where/script/is/useArray.php " . escapeshellarg($data) . " &");


话虽如此,我还是从一开始就避免这种调用,并使用其他通信机制,如使用ØMQ、Gearman等的队列/工作者设置。但这超出了本主题的范围。

您是通过shell语法来解释字符串的,它有自己非常大和复杂的特殊字符集。例如,
引号由shell解释,因此从结果值中去掉

如果您想通过shell(或实际上通过任何具有自己语法和特殊字符的中间语言)传输任意字符串,则需要适当地转义它:

exec("php path/where/script/is/useArray.php " . escapeshellarg($data) . " &");


说到这里,我首先要避免这种调用,并使用其他通信机制,比如使用ØMQ、Gearman等的队列/工作者设置。但这超出了本主题的范围。

您通常无法在shell中键入随机字符并将其作为常规文本传递,这就是存在的原因(尽管根据我的经验,它只能在Unix shell上正常工作,并且在Windows上经常出现严重故障)

在任何情况下,命令行参数仅适用于小参数。如果需要传输复杂数据,最好使用其他机制:

  • 标准输入
  • 临时文件
对于前者,您必须转储
exec()
,并使用例如
proc\u open()
——您可以在中找到使用示例


对于后者,只需从中选择您最喜欢的。对于小文件,
file\u put\u contents()
/
file\u get\u contents()
组合可能很好。

您通常无法在shell中键入随机字符并将其作为常规文本传递,这就是为什么存在的原因(尽管根据我的经验,它只能在Unix shell上正常工作,在Windows上经常失败)

在任何情况下,命令行参数仅适用于小参数。如果需要传输复杂数据,最好使用其他机制:

  • 标准输入
  • 临时文件
对于前者,您必须转储
exec()
,并使用例如
proc\u open()
——您可以在中找到使用示例

对于后者,只需从中选择您最喜欢的。对于小文件,
file\u put\u contents()
/
file\u get\u contents()
组合可能很好