It so happened that in the do while , requests are sent to API Vkontakte API , how to make a pause between these requests?

Requests are sent using file_get_contents

That's all the code

 do { $response = file_get_contents("http://api.vk.com/method/likes.getList?type=".$myrow_table_likes["type"]."&owner_id=".$myrow_table_likes["owner_id"]."&item_id=".$myrow_table_likes["item_id"]."&filter=likes"); $resp = json_decode($response, true); if (!in_array($user_uid, $resp['response']['users'])) { echo $myrow_table_likes["id"]."No likes<br>"; } } while($myrow_table_likes = mysql_fetch_array($result_bd_likes)); 
  • one
    usleep() - etki
  • @Etki I tried, everything hangs blankly and then flies by in an instant - Anatoly
  • did you exactly put it inside do? - etki
  • @Etki Changed the Question, wrote the Code in, see. - Anatoly
  • for file_get_contents it is better to specify the context with the connection: close (like so), otherwise you will get an answer in a split second and wait 30 seconds for 30 manna from heaven - BOPOH

2 answers 2

How could I have ported this answer from python to php

This code sends 20 messages with a frequency of 5 in 10 seconds:

 <?php $start = time(); $rate = 5.0; // $rate Сообщений $per = 10.0; // за $per секунд $allowance = $rate; // сколько сначала можно отправить $last_check = time(); // floating-point, eg usec accuracy. Unit: seconds for($i = 0; $i < 20;) { $current = time(); $time_passed = $current - $last_check; $last_check = $current; $allowance += $time_passed * ($rate / $per); if ($allowance > $rate) { $allowance = $rate; // throttle } if ($allowance < 1.0) { sleep(1); continue; } else { $i++; echo "working! elapsed: ".(time() - $start).PHP_EOL; $allowance -= 1.0; } } ?> 

    The easiest option. We put before the request

     sleep(3);