I want to make a bot in the personal messages of the community that would respond to certain messages with a specific answer. That is, if the user sends the digit "1", then he receives the corresponding instruction, if "2", then another and so on.

With my knowledge (and with the help of Google) I managed to make this option:

$messageText = $data->object->body; $userId = $data->object->user_id; $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); $user_name = $userInfo->response[0]->first_name; // Условие для 1 if (trim($messageText) === "1") { $request_params = array( 'message' => "Привет, {$user_name}!<br>" . "Текст для 1", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); //Возвращаем "ok" серверу Callback API echo('ok'); } // Условие для 2 elseif (trim($messageText) === "2") { $request_params = array( 'message' => "Привет, {$user_name}!<br>" . "Текст для 2", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); //Возвращаем "ok" серверу Callback API echo('ok'); } 

This code has already been verified, and it works. But can not cope with the task. The fact is that the messages that are sent to users are large. And when building a query, apparently, the limit on the number of characters in the file_get_contents method file_get_contents .

I tried to do on CURL (for examples of documentation):

 if (trim($messageText) === "1") { $msg = "Hello, world"; //Функция для вызова любого метода API function _vkApi_call($method, $params = array()) { $params['access_token'] = $token; $params['v'] = VK_API_VERSION; $url = VK_API_ENDPOINT.$method.'?'.http_build_query($params); $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $json = curl_exec($curl); curl_close($curl); $response = json_decode($json, true); return $response['response']; } //Функция для вызова messages.send function vkApi_messagesSend($peer_id, $message, $attachments = array()) { return _vkApi_call('messages.send', array( 'peer_id' => $peer_id, 'message' => $message, 'attachment' => implode(',', $attachments) )); } vkApi_messagesSend($userId, $msg); echo('ok'); } 

The message from the user is not read. In requests for VK: Error: HTTP response code said error . Unfortunately, my knowledge is not enough here (do not judge strictly, everyone started with something).

Actually the question: how to send a large message?

    1 answer 1

    try to send via the POST method

     //Функция для вызова любого метода API function _vkApi_call($method, $params = array()) { $params['access_token'] = $token; $params['v'] = VK_API_VERSION; $url = VK_API_ENDPOINT . $method; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); $json = curl_exec($ch); curl_close($ch); $response = json_decode($json, true); return $response['response']; } 
    • Unfortunately, there was no result ... - Ivashko Derevyashko