Answer the question
In order to leave comments, you need to log in
Why random_id in vk api refuses to work normally?
Greetings friends, initially the problem was that the bot sends one message several times, the reason for this was the random_id parameter, which should be unique each time, and I had 'random_id' => 0. Now that the problem is found, I'm trying to generate random_id, but for some reason the bot still sends multiple messages. There is no problem with the uniqueness of $random_id, since I output it through var_damp and see that each time a new number. The strange thing is that if I manually set the number to some value, for example, 1, 2, 3, etc. then everything works, and it sends 1 time, only after that it needs to be changed every time, but again, if you do it manually, then everything works, so why doesn’t it work with normal generation. I am attaching the code below:
$random_id = rand(1,9999999999999999);
// Отправляемое сообщение
$message = 'Здравствуйте, ' . $user_name . '! Вы запросили привязку данного аккаунта к аккунту на ресурсе SUDRUS. Ваш код подтверждения ' . $code . '. Введите его на сайте, чтобы подтвердить этот метод отправки уведомлений.';
function send($id, $msg, $token, $version, $random_id){
$url = 'https://api.vk.com/method/messages.send';
$params = array(
'user_id' => $id,
'message' => $msg,
'access_token' => $token,
'v' => $version,
'random_id' => $random_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)
)
)));
}
send($user_id, $message, $token, $api_version, $random_id);
var_dump($random_id);
echo 'OK';
Answer the question
In order to leave comments, you need to log in
the bot sends one message several times, the reason for this was the random_id parameter, which must be unique each time, and I had 'random_id' => 0.
I'm trying to generate a random_id, but for some reason the bot still sends a few messages.
Try reducing the number of nines to something like:
I've come across this, I can't professionally describe why, but it works.
In any case, try to remove the nine, until you succeed.
The problem seems to be that the rand function does not support numeric types with large values.
$random_id = rand(1,99999999);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question