M
M
Murka Murenich2017-09-24 15:18:26
PHP
Murka Murenich, 2017-09-24 15:18:26

How to remove Incorrect response text error on failed CallBack API requests?

With each message, this error pops up in failed requests:
Incorrect response text
Here is the response:
HTTP/1.1 200 OK
Server: nginx-reuseport/1.13.2
Date: Sun, 24 Sep 2017 11:02:57 GMT
Content-Type: text/html
Content-Length: 0
Connection: keep-alive
Keep-Alive: timeout=30
X-Powered-By: PHP/5.6.30
Here is the whole code:
bot.php:

<?php
require __DIR__.'/config/config.php';
require __DIR__.'/core/apiVK.php';
$v = new vk();
$confirmation_token = 'токен';
//Ключ доступа сообщества
$token = 'токен';

if (!isset($_REQUEST)) {
  return;
}


//Получаем и декодируем уведомление
$data = $v->get();

//Проверяем, что находится в поле "type"
switch ($data->type) {
  //Если это уведомление для подтверждения адреса сервера...
  case 'confirmation':
    //...отправляем строку для подтверждения адреса
    echo $confirmation_token;
    break;

//Если это уведомление о новом сообщении...
  case 'message_new':
    //...получаем id его автора
    $uid = $data->object->user_id;
  $user_msg = $data->object->body;

    //затем с помощью users.get получаем данные об авторе
    $user_info = $v->usersGet($uid);

//и извлекаем из ответа его имя
  $info = array_shift(json_decode($user_info)->response);
  $uname = $info->first_name;

  //С помощью messages.send и токена сообщества отправляем ответное сообщение

  //Главное Меню
  $user_msg = mb_strtolower($user_msg);
    if (strpos($user_msg, 'команда 1') !== false or strpos($user_msg, 'команда 1-1') !== false or strpos($user_msg, 'команда 1-1-1') !== false) {
      $v->msgsend("ответ на команду", $uid, $token, $attachment);

}elseif ($user_msg == 'команда 2'){
  $v->msgsend("ответ на команду", $uid, $token, $attachment);


// Ответ бота на не понятную команду.
     }else{
     $v->msgsend("
     Я тебя не понимаю.", $uid, $token);
     }
//Возвращаем "ok" серверу Callback API
    echo('ok');
die;
break;
}
?>

apiVK.php:
<?php

class vk {
  const API_VERSION = '5.24';
  const METHOD_URL = 'https://api.vk.com/method/';

  public function get(){
    return json_decode(file_get_contents('php://input'));
  }
  
  public function usersGet($uid){	
    return file_get_contents(self::METHOD_URL."users.get?user_ids={$uid}&v=".self::API_VERSION);
  }
  
    public function msgSend($msg, $uid, $token, $attachment){	
    $request_params = array(
      'message' => $msg,
      'attachment' => $attachment,
      'user_id' => $uid,
      'access_token' => $token,
      'v' => self::API_VERSION
    );
    $get_params = http_build_query($request_params);
    file_get_contents(self::METHOD_URL."messages.send?".$get_params);
  }
  

}

?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Serezha, 2017-09-24
Ahen @Ahen

Give the correct answer to the request?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question