Answer the question
In order to leave comments, you need to log in
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
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");
}
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);
}
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 questionAsk a Question
731 491 924 answers to any question