加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

限制传出PHP卷曲请求

发布时间:2020-12-13 16:18:10 所属栏目:PHP教程 来源:网络整理
导读:有没有办法将传出的 PHP curl请求限制(延迟)到外部服务器,这样每秒只有n个请求? PHP用于Fastcgi模式,因此无法使用睡眠. 解决方法 是.有卷曲多处理程序…… (您可以使用this library以OOP方式进行) 例如: // Creates the curl multi handle $mh = curl_mult
有没有办法将传出的 PHP curl请求限制(延迟)到外部服务器,这样每秒只有n个请求? PHP用于Fastcgi模式,因此无法使用睡眠.

解决方法

是.有卷曲多处理程序……

(您可以使用this library以OOP方式进行)

例如:

// Creates the curl multi handle
    $mh = curl_multi_init();
    $handles = array();

    foreach($urls as $url)
    {
        // Create a new single curl handle
        $ch = curl_init();

        // Set options
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_TIMEOUT,300);

        // Add to the multi handle
        curl_multi_add_handle($mh,$ch);

        // Put the handles in an array to loop this later on
        $handles[] = $ch;
    }

    // Execute the multi handle
    $running=null;

    do
    {
        $mrc = curl_multi_exec($mh,$running);

        // Added a usleep for 0.50 seconds to reduce load
        usleep (250000);
    }
    while($running > 0);

    // Get the content of the urls (if there is any)
    $output = array();
    for($i=0; $i<count($handles); $i++)
    {
        // Get the content of the handle
        $content = curl_multi_getcontent($handles[$i]);
        $output[] = $content;

        if($printOutput) {
            echo $content;
        }

        // Remove the handle from the multi handle
        curl_multi_remove_handle($mh,$handles[$i]);
    }

    // close the multi curl handle to free system resources
    curl_multi_close($mh);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读