延迟请求cURL

时间:2018-01-28 17:19:05

标签: php curl

我每隔一小时使用cron作业运行以下代码,但这会导致服务器上所有网站的页面加载速度变慢。所以我想在每个请求之间添加一个时间延迟,但我不知道该怎么做。

    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>';
}

1 个答案:

答案 0 :(得分:0)

$ time是第二个

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>';
}