Answer the question
In order to leave comments, you need to log in
Callbackapi vk, php, mysql: what is wrong with this code?
I took from the example https://vk.com/dev/callback_api
And changed it a little - so that there was a record in the database.
As a result, I get several entries in the database for one event, and they also go interspersed: after the second, third, etc. can record the previous one again - as in the picture.
What's wrong with the code?
<?php
//var_dump($_REQUEST);
if (!isset($_REQUEST)) {
return;
}
$txt = '';
$date = '';
//Строка для подтверждения адреса сервера из настроек Callback API
$confirmation_token = 'dkrijsakcnalfn';
//Ключ доступа сообщества
$token = 'trjhrtjrtjjrtjrjurtererherjherhe';
//Получаем и декодируем уведомление
$data = json_decode(file_get_contents('php://input'));
//Проверяем, что находится в поле "type"
switch ($data->type) {
//Если это уведомление для подтверждения адреса сервера...
case 'confirmation':
//...отправляем строку для подтверждения адреса
echo $confirmation_token;
break;
//Если это уведомление о новом сообщении...
case 'wall_post_new':
//...получаем
$txt = $data->object->text;
$date = $data->object->date;
//Возвращаем "ok" серверу Callback API
echo('ok');
break;
}
require('bd.php');
$host = "localhost";
$user = "user";
$password = "hdrgeegse";
$datebase = "dbname";
updateDate ($host, $user, $password, $datebase, $txt, $date);
?>
<?php
function updateDate ($host, $user, $password, $datebase, $txt, $date) {
$link = mysqli_connect($host, $user, $password, $datebase) or die("Не могу соединиться с MySQL.");
$query = "INSERT INTO `text`(`id`, `text`, `date`) VALUES (NULL,$txt,$date)";
$result = mysqli_query ($link, $query) or die("ERROR! " . mysqli_error($link));
if($result)
{
echo "<span style='color:blue;'>OK!</span>";
}
// закрываем подключение
mysqli_close($link);
}
Answer the question
In order to leave comments, you need to log in
Che? Maybe it's better not to... continue?
And in the case - in the output for callback vk there should be nothing but "ok".
You have the same conclusion "ok<span style='color:blue;'>OK!</span>"
, VK considers this an error and continues to send notifications in the hope that you can finally process it.
What for?!
if($result)
{
echo "<span style='color:blue;'>OK!</span>";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question