B
B
baryshevm345342020-04-17 04:15:40
PHP
baryshevm34534, 2020-04-17 04:15:40

PHP Curl Bad Request JSON - problem in message - how to format correctly?

there is a date array that is passed to be sent via curl to a function

$arData = [
  "NAME" => 'Название конференции',
  "CODE" => "",
  "START" => '18.04.19 11:11:10',
];
telegramNotificationSend($id, $typeNotification, $arData);

in the send function there is a code that generates a message

$textNotification = " Конференция " . $arData["NAME"] . " скоро начнется. Время начала в " . $arData["START"];

When curl is executed, Bad Request is written in response.

If you send just a text string, without substituting information from the arData array, then everything is sent correctly

$textNotification = $dateStart . " Конференция скоро начнется. Время начала в ";

send code

$data_string = json_encode ($data, JSON_UNESCAPED_UNICODE);
  $curl = curl_init(TELEGRAM_BOT_URL);
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
  // Принимаем в виде массива. (false - в виде объекта)
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER, array(
             'key: ' . TELEGRAM_BOT_KEY,
             'Content-Type: application/json',
             'Content-Length: ' . strlen($data_string))
  );
  $result = curl_exec($curl);
  curl_close($curl);

Even if you just equate $textNotification = $arData["NAME"], then there will also be a Bad Request error.

What could be the problem? In hidden unsupported characters during transmission? But I just equated one line to another, where can they come from?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
baryshevm34534, 2020-04-17
@baryshevm34534

Issue resolved:
1) encode json without JSON_UNESCAPED_UNICODE
2) add charset=utf-8 to header

$data_string = json_encode ($data);
'Content-Type: application/json; charset=utf-8',

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question