php – Problem with VK API and cURL

Question:

I want to make a bot in private messages of the community that would react to certain messages with a specific answer. That is, if the user sends the number "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 the following 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 tested and it works. But it does not cope with the task. The point 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 is file_get_contents .

I tried to do on cURL (according to the documentation examples):

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 cannot be read. In VK requests: Error: HTTP response code said error . Unfortunately, my knowledge is not enough here (do not judge strictly, everyone started somewhere).

The actual question is: how to send a big message?

Answer:

try to send through 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'];
}
Scroll to Top