saeed golreyhan
saeed golreyhan

Reputation: 23

Delay in requests cURL

Im Running the below code with cron jobs every one hour but it causes the slow page load for all websites on the server. So I wanted to add a time delay between every request but I dont know how to do it exactly.

    public function getAllUsersStats() {
    $users = \App\Models\User::all();
    echo '<pre>';
    foreach($users as $user) {
        $urls[] = 'https://www.vowave.com/stats-cron/' . $user->id;
        $urls[] = 'https://www.vowave.com/stats/cpwq?daily=true&uid=' . $user->id;
        $urls[] = 'https://www.vowave.com/stats/cpwq?hourly=true&uid=' . $user->id;
        foreach($urls as $url) {
            echo $url . PHP_EOL;
            bg_get_url($url);
        }
    }
    echo '</pre>';
}

Upvotes: 0

Views: 1351

Answers (1)

Ganesh Kandu
Ganesh Kandu

Reputation: 621

$time is second

usleep($time * 1000000);

public function getAllUsersStats() {
    $users = \App\Models\User::all();
    echo '<pre>';
    foreach($users as $user) {
        $urls[] = 'https://www.vowave.com/stats-cron/' . $user->id;
        $urls[] = 'https://www.vowave.com/stats/cpwq?daily=true&uid=' . $user->id;
        $urls[] = 'https://www.vowave.com/stats/cpwq?hourly=true&uid=' . $user->id;
        foreach($urls as $url) {
            echo $url . PHP_EOL;
            usleep(0.5 * 1000000);//sleep for 2.5 second for each requiest
            bg_get_url($url);
        }
    }
    echo '</pre>';
}

Upvotes: 0

Related Questions