API vk.com - Wrong encoding in title of link attachments

292 Views Asked by At

I use "Callback API" of vk.com

VK send http request to my server when somebody post in my vk group.

Text of post has right encoding, but title of link attachments has wrong encoding. Why?

Here code of php script which I set in "Callback API" url:

<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);
header('Content-Type: text/html; charset=utf-8');

$data = file_get_contents('php://input');
file_put_contents("log.txt", $data . PHP_EOL, FILE_APPEND);

Inside log.txt I see:

{"group_id":216114360,"type":"wall_post_new","event_id":"d40825fa7c9b532aea813ec19bad60e68cd8a7a0","v":"5.131","object":{"id":6,"from_id":-216114360,"owner_id":-216114360,"date":1663854048,"marked_as_ads":0,"can_delete":1,"is_favorite":false,"post_type":"post","text":"Тут все в порядке с кодировкой. Но почему есть проблемы с кодировкой в title attachment?\nhttps:\/\/www.vesti.ru\/","can_edit":1,"created_by":17069557,"attachments":[{"type":"link","link":{"url":"https:\/\/www.vesti.ru\/","title":"?4??4??4??4??1?.Ru: ?4??4??4??4??4??4??1?, ?4??4??4??4??1? ?1? ?4??4??4??1? ?4??4??5?","caption":"www.vesti.ru","description":"?4??4??4??4??4??4??1? ?4??4??5? ?4??1? ?4??4??4??4??1?.Ru, ?4??4??4??4??4??4??5??5?, ?4??4??4??4??4??4??4??4??1?, ?4??4??4??1? ?1? ?4??4??4??4??1?, ?4??4??4??4??4??4??1? ?4??4??4??4??4??5? ?1? ?4??4??4??4??4??4??4??1? ?4??4??4??4??4??1?, ?4??4??4??4??4??4??1? ?5??4??4??4??4??4??4??4??1?, ?4??4??4??4??4??6?","photo":{"album_id":-27,"date":…}

['attachments'][0]['link']['title'] - has wrong encoding.

But ['object']['text'] - has right encoding.

If I get post using api vk encoding is right. Code:

<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);
header('Content-Type: text/html; charset=utf-8');

$access_token = "set here access_token";
$url = $url = "https://api.vk.com/method/wall.get?v=5.131&domain=r_bobruisk&access_token=" . $access_token;
$data = file_get_contents($url);
$data = json_decode($data, true);
foreach ($data['response']['items'] as $item) {
    if (isset($item['attachments'][0]['link']['title'])) {
        file_put_contents("log.txt", date("d.m.Y H:i:s") . " " . $item['attachments'][0]['link']['title'] . " " . PHP_EOL, FILE_APPEND);
        echo $item['attachments'][0]['link']['title'];
        echo "<br>";
    }
}

log.txt:

22.09.2022 16:25:42 требуется педагог-психолог 
22.09.2022 16:25:42 Требуется дворник 
22.09.2022 16:25:42 Кассир 
22.09.2022 16:25:42 Шаурмист 
22.09.2022 16:25:42 Требуется продавец 
22.09.2022 16:25:42 Кладовщик 
22.09.2022 16:25:42 Требуется на работу 
22.09.2022 16:25:42 Требуется торговый агент 

How I post to my vk group:

enter image description here

Result of post:

enter image description here

I have wrong encoding only when I use "Callback API". If I get post using vk api encoding is right.

2

There are 2 best solutions below

1
cetver On

You need to address the "Why encodings are different ?" question to VK support.

Convert the encoding yourself:

$data = file_get_contents('php://input');
$utfData = mb_convert_encoding($data, 'UTF-8');
1
Ikenitenine On

I have asked this question to VK support 3 months ago. I have run my script today - its work well, but I haven’t change its since time. I think VK have fixed this issue.