L
L
lucifer_jr2018-02-17 00:39:30
PHP
lucifer_jr, 2018-02-17 00:39:30

When I send a long message in a loop using the VK API, the loop behaves illogically. Why?

foreach ($arr_for_send as $value) {
sleep(10);
send($data->object->user_id, $value);
}
but the bot floods me with messages, sending the same message dozens of times

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Ernest Faizullin, 2018-02-17
@lucifer_jr

most likely the send function does not return anything, you should always return the status 200 ok
at the end of the send function something like

public function send($user_id, $value) {

    ...

   return header("HTTP/1.1 200 OK");
}

UPDATE: so the problem is that with a delay of a few seconds, many apis regard this as a failed response and try to get the response again and again.
Possible solution:
Before each delay in the cache, put a flag that the application is frozen and, inside the send function or after the delay, unfreeze the application by removing this label. Example
if (Cache::has('app_delayed')) {
    return 'ok';
}

foreach ($arr_for_send as $value) {

    Cache::put('app_delayed', true);
    sleep(10);
    Cache::forget('app_delayed');

    send($data->object->user_id, $value);
}

V
VK API, 2018-02-17
@vkapi

The server should receive a response to the request immediately. If there is no response for a long time, the request is considered unsuccessful and is repeated again. Therefore, you need to either reduce the script execution time, for example, using execute, or simply answer 'ok', write the event to the database, and then process it with another script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question