A
A
Anickdot2017-10-08 23:45:36
PHP
Anickdot, 2017-10-08 23:45:36

How to delay the execution of a function in php?

I am writing a bot in php for VK. The task is as follows: the bot writes "Hello", he answers "Bye", but only after 2 hours.
Here is the part of the code that is responsible for sending a response message if they write "Hi".

case strstr($message,'Привет') :
  
   		 $request_params = array( 
   		 	'message' => "Пока", 
   		 	'user_id' => $user_id, 
   		 	'access_token' => $token, 
   		 	'v' => '5.0' 
   		 ); 
   		 $get_params = http_build_query($request_params);

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://api.vk.com/method/messages.send'); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $get_params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);curl_close($ch); break;

If everything is left in this form, then everything is fine - the bot works, responds instantly and life is good.
How to force it to do it in an hour/two after reception of the message?
Sleep(10) - works. (but as I understand it, this function causes it to freeze for 10 seconds anywhere in the script)
And if you set it more, then requests begin to pour in several times, the bot can start sending messages 3-4 times.
Which way to think?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Sklyarov, 2017-10-08
@0example

Think CRON

A
Alexey Skobkin, 2017-10-08
@skobkin

Cron or queue servers + workers.

D
Danil Razumkov, 2018-01-16
@razumkov2015

DB + CRON

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question