1
1
1Soundwave32016-10-18 16:05:33
PHP
1Soundwave3, 2016-10-18 16:05:33

VK API Not sending Cyrillic?

Hello!
I want to send PM using php script.
I took the script from here https://habrahabr.ru/post/265563/ tried to run it and as a result, when sending the word "check" comes ?4??4??4??4??4??4??4?? 6? or ?4??4??4??6? in the case of the word "test". In general, Cyrillic is not sent. I tried urlencode("verify") but it didn't work. Latin comes at its best.
Here is the function code from the post for convenience:

function send($id , $message)
{
    $url = 'https://api.vk.com/method/messages.send';
    $params = array(
        'user_id' => $id,    // Кому отправляем
        'message' => $message,   // Что отправляем
        'access_token' => '0000000000000000000000000000',  // access_token можно вбить хардкодом, если работа будет идти из под одного юзера
        'v' => '5.37',
    );
 
    // В $result вернется id отправленного сообщения
    $result = file_get_contents($url, false, stream_context_create(array(
        'http' => array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => http_build_query($params)
        )
    )));
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Petr Flaks, 2016-10-18
@1Soundwave3

You probably have a PHP file with encoding other than UTF-8.
Also use more modern tools like cURL:

<?php

function send($id, $message) {
  $ch = curl_init();
  $parameters = http_build_query([
    'user_id'      => $id,
    'message'      => $message,
    'access_token' => 'fa0mfbc2b13c3104f48fd2g75e0a770b34119c89f0e16f75e1502f03e9346413c10b8ad428737496f5602',
    'v'            => '5.59'
  ]);

  curl_setopt($ch, CURLOPT_URL, 'https://api.vk.com/method/messages.send');
  curl_setopt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);

  // Результат запроса к API
  $result = curl_exec($ch);

  curl_close($ch);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question