skip to Main Content

I have a textarea value which i need to save into database as it ..
Suppose the textarea value is like this:

I’m State Representative Naydu. I am sending you this message to let
you know that I have filed for re-election today. I hope I can count
on your support for our campaign!
https://secure.actblue.com/donate/reelectnaydu?refcode=filingtxt

Reply STOP to opt out

So I want to save it in database like this,

I’m State Representative John Bryant. I am sending you this message to
let you know that I have filed for re-election today. I hope I can
count on your support for our campaign!
nnhttps://secure.actblue.com/donate/reelectnaydu?refcode=filingtxtnnReply
STOP to opt out

I have saved it with this $communication->communication_text = $request->message; line. But can’t save it as expected. How can I do it?

I want to send the variable to a Curl request to send API request like this

 CURLOPT_POSTFIELDS =>'{
                        "bulkId": "'.$communication->id.'",
                        "messages": [
                        {
                        
                        "destinations": [
                        ' . implode(',', $destinations) . '
                        ],
                        "from": "'. $sender->sender_number.'",     
                        "text": ".$communication->communication_text."
                        }
                        ],
                        "tracking": {
                        "track": "SMS",
                        "type": "MY_CAMPAIGN"
                        }
                        }',

But I got parsing Error:

"{"requestError":{"serviceException":{"messageId":"BAD_REQUEST","text":"Error
while parsing request body. Please check your syntax."}}}"

Here is the snap of $communication->communication_text, If i hard coded the text everythings works fine, when i pass it from controller i got parsing Error.
enter image description here

2

Answers


  1. You can use the base64 encoding to store the data in a database. If you want to retrieve the data convert it into base64 decode(base64_decode).

    $communication->communication_text = base64_encode($request->message);

    Login or Signup to reply.
  2. Your problem is that you’re constructing JSON by hand. This will usually always lead to errors, whether it be by unescaped special characters or quote marks.

    You’re in luck though, because there is a solution: json_encode.

    Simply, you can do this for your API request:

    $destinationsMapped = [];
    foreach ($destinations as $destination) {
      $destinationsMapped[] = ['to' => (string) $destination];
    }
    
    $data = [
        'bulkId' => $communication->id,
        'messages' => [
            [
                'destinations' => $destinationsMapped,
                'from' => $sender->sender_number,
                'text' => $communication->communication_text
            ]
        ],
        'tracking' => [
            'track' => 'SMS',
            'type' => 'MY_CAMPAIGN'
        ]
    ];
    
    // ...
    CURLOPT_POSTFIELDS => json_encode($data)
    //...
    

    Note that the array format is based on the data you gave in your question, but you might still need to adjust it. json_encode takes care of the JSON-ification for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search