F
F
Franco Mannino2017-05-12 16:45:11
PHP
Franco Mannino, 2017-05-12 16:45:11

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);		
}

e56a4f81b42444839335ec81c4515d82.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Serezha, 2017-05-12
@garinov

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>";
    }

Plus - WHY are you writing to the database outside the case? What if it's a server validation request? What for then to make record in a DB?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question