简体   繁体   中英

Facebook Messenger API line break JSON error

A bit new to JSON, but I understand the basics... I have a system that integrates with the Facebook Messenger API and the Webhook page simply dumps the data into my MySql database so I can process the data on my system. Everything works normally until a user sends a message with a line break. I can't post all my code, but here is the basics:

My_Webhook_File.php

$input = file_get_contents('php://input');
$data = json_decode($input, true);
$sender = $data['entry'][0]['messaging'][0]['sender']['id'];
$message = $data['entry'][0]['messaging'][0]['message']['text'];
$message_id = $data['entry'][0]['messaging'][0]['message']['mid'];
$recipient = $data['entry'][0]['messaging'][0]['recipient']['id'];

if(!empty($sender)){
     $db = db("INSERT INTO FB (senderid, sender_name, recipient, the_time, data, is_read) VALUES ('".numbersonly($sender)."', '".$fbname."', '".$recipient."', NOW(), '".json_encode($data)."', '".$is_read."')");
}

Everything works normally until a user sends text with a new line (SHIFT + ENTER) directly from FB. I have tried dumping only the contents of "$input" into the database and still get the same result. How would I fix this issue? I tried nl2br which did not work at all.

Example of what is returned:

{"object":"page","entry":[{"id":"MY_PAGE_ID","time":1672599591761,"messaging":[{"sender":{"id":"SENDER_ID"},"recipient":{"id":"RECIPIENT_ID"},"timestamp":1672599591554,"message":{"mid":"MESSAGE_ID","text":"maybe
this
will

work"}}],"hop_context":[{"app_id":MY_APP_ID,"metadata":""}]}]}

How it should look:

{"object":"page","entry":[{"id":"MY_PAGE_ID","time":1672599591761,"messaging":[{"sender":{"id":"SENDER_ID"},"recipient":{"id":"RECIPIENT_ID"},"timestamp":1672599591554,"message":{"mid":"MESSAGE_ID","text":"maybe\nthis\nwill\nwork"}}],"hop_context":[{"app_id":MY_APP_ID,"metadata":""}]}]}

So after loads of researching, I figured a work around. Here is the updated method.

I set the SQL Column "data" to a Text type instead of JSON and then used str_replace() to fix the extra lines. There may be a cleaner method to this, but it seems to be working:)

<?php
// $message['data'] is directly from the DB

// replace all carriage returns with ":linebreak" to avoid converting <br /> in htmlentities 
$data = str_replace(array("\r", "\n"), ':linebreak:', $message['data']);
$m = json_decode($data);
if(is_array($m->entry[0]->messaging[0]->message->attachments ?? null)){
    foreach($m->entry[0]->messaging[0]->message->attachments as $obj){
        if($obj->type == "image"){
?>
            <a href="<?php echo $obj->payload->url;?>" target="_blank"><img src="<?php echo $obj->payload->url;?>" width="100%"></a><br />
<?php
        }
    }
}
// clean up any potential HTML code the user may have sent or characters that can affect the output
$parsed_data = htmlentities(stripslashes($m->entry[0]->messaging[0]->message->text));
// now that htmlentities is done, replace ":linebreak:" with <br />
$data = str_replace(':linebreak:', '<br />', $parsed_data);
echo $data;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM