L
L
LebedevImagine2019-12-22 12:41:48
API
LebedevImagine, 2019-12-22 12:41:48

Why is the server not responding to a request from the VK API?

Good afternoon fellow programmers.
I'm trying to create the simplest Vkontakte bot in PHP, according to the instructions, but something goes wrong.
The bottom line is that my server responds to the check and responds with the desired string (confirmation event), but it does not respond to other events. (In the settings of the Vkontakte community it is clearly written that the request was sent) 5dff399067aaf484651800.png
The code is as follows:

<?php

$confirmToken = '450f8570';
$token = 'мой токен';
$group_id = 190089312;
$secretKey = 'мое секретное слово';
$data = json_decode(file_get_contents('php://input'));


switch ($data->type) {
    case 'confirmation':
        echo $confirmToken;
        break;
    case 'message_new':
        $userId = $data->object->from_id;
        $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.103"));
        $user_name = $userInfo->response[0]->first_name;

        //С помощью messages.send и токена сообщества отправляем ответное сообщение
        $request_params = array(
            'message' => "{$user_name}, ваше сообщение зарегистрировано!<br>".
                            "Мы постараемся ответить в ближайшее время.",
            'user_id' => $userId,
            'access_token' => $token,
            'v' => '5.103'
        );

        $get_params = http_build_query($request_params);

        $result = file_get_contents('https://api.vk.com/method/messages.send?' . $get_params);
        
        setcookie('API', 'true');

        var_dump($result);
        
        //Возвращаем "ok" серверу Callback API
        echo('ok');

        break;
}

As a debug, I create cookies in order to check if the server accepted my request or not. As a result, only system cookies are stored in the browser and nothing else. What could be the problem? Where to dig? Tell the unfortunate programmer.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lightmanLP, 2019-12-22
@LebedevImagine

$userId = $data->object->message->from_id;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question