使用json_encode php的不寻常行为

时间:2018-02-23 16:08:50

标签: java android

我的服务器API在发布请求中接收来自客户端的消息,将它们存储在表中,然后通过curl请求使用firebase通知将消息发送到目标客户端。

如果我从我的Android应用程序向我的服务器发送消息,示例@RunWith(AndroidJUnit4.class) public class FooTest { @Test public void testFoo() { .... } } (有一个引号),服务器会收到该消息并将其存储在数据库中,但我使用了This message doesn't work所以它会被存储好,而不会中断我的查询。当我查看数据库时,我看到addslashes()(注意单引号在那里)

然后,我使用我想发送给目标客户端的数据构建一个数组:

1  |  user_1  | user_2  |  This message doesn't work

但由于某种原因,即使我的data.log文件包含

    //We are going to insert the new message into the database, assign all the variables a shortname
    $from_id = $user['id'];
    $to_id = $_POST['to_user_id'];
    $body = $_POST['message_body'];
    $from_name = $user['firstname'] . ' ' . $user['lastname'];

    //Build a query to insert into the `user_messages` table
    $query = "INSERT INTO `user_messages` (`from_user_id`,`to_user_id`,`message_body`) VALUES ('". $from_id ."','". $to_id ."','". addslashes($body) ."')";

    //Execute the query to insert the new message
    mysqli_query($conn, $query);

    //Now we want to proceed to send the message on to its destination, get the destination installation ID
    $query = "SELECT * FROM application_tokens WHERE user_id=" . $to_id;
    $tokens[] = mysqli_fetch_array(mysqli_query($conn, $query), MYSQLI_ASSOC)['application_token'];

    //With the token, we want to build the string of data that gets sent to the FCM notifications server
    $NotificationData = array(
            "data" => array(
                    "notification_type" => "message",
                    "from_user_id" => $from_id,
                    "from_user_name" => $from_name,
                    "sent_time" => date('H:i'),
                    "message_body" => $body,
            ),
            "registration_ids" => $tokens,
    );

    //Broadcast the message
    SendFCM($notifications_key, $NotificationData);

    function SendFCM($notifications_key, $NotificationData){
            $command = "curl -X POST --Header 'Authorization: key=". $notifications_key  ."' --Header 'Content-Type: application/json' -d '" . json_encode($NotificationData) . "' 'http://fcm.googleapis.com/fcm/send'";

            shell_exec($command);

            file_put_contents("/var/www/html/MobileAppAPI/data.log", $command);
    }

更具体地说

curl -X POST --Header 'Authorization: key=AIzaSyCUQWJWypXPXxDV2s3wfNPprx5Kiu0uvho' --Header 'Content-Type: application/json' -d '{"data":{"notification_type":"message","from_user_id":"7","from_user_name":"test device2","sent_time":"15:56","message_body":"test\\'"},"registration_ids":["cvmM2CufUWE:APA91bGrEgT6oMoedJK-CxZDDRCu356E5MteYHppqFxBU4DWOaNOU6sVAZ1xzaRfSDtUdbRpGz1WExuMMELGA6vit-d9_Sxim_OhRvESGRzNPJp-5DMzuavMKbwFIPCSASed2GJKLjg8"]}' 'http://fcm.googleapis.com/fcm/send'

似乎已正确转义,邮件永远不会发送到客户端。事实上,我不认为命令会被执行,因为当我手动运行数据日志的内容时,它会因某个地方缺少“”而被卡住。

我是否错误地逃过单引号?没有单引号的消息可以通过OK。

感谢。

0 个答案:

没有答案