• 技术文章 >后端开发 >php教程

    使用curl_multi实现并发请求步骤分析

    php中世界最好的语言php中世界最好的语言2018-05-16 11:47:17原创1825
    这次给大家带来使用curl_multi实现并发请求步骤分析,使用curl_multi实现并发请求的注意事项有哪些,下面就是实战案例,一起来看一下。

    class CurlMultiUtil {
      /**
      * 根据url,postData获取curl请求对象,这个比较简单,可以看官方文档
      */
      private static function getCurlObject($url,$postData=array(),$header=array()){
        $options = array();
        $url = trim($url);
        $options[CURLOPT_URL] = $url;
        $options[CURLOPT_TIMEOUT] = 3;
        $options[CURLOPT_RETURNTRANSFER] = true;
        foreach($header as $key=>$value){
          $options[$key] =$value;
        }
        if(!empty($postData) && is_array($postData)){
          $options[CURLOPT_POST] = true;
          $options[CURLOPT_POSTFIELDS] = http_build_query($postData);
        }
        if(stripos($url,'https') === 0){
          $options[CURLOPT_SSL_VERIFYPEER] = false;
        }
        $ch = curl_init();
        curl_setopt_array($ch,$options);
        return $ch;
      }
      /**
       * [request description]
       * @param [type] $chList
       * @return [type]
       */
      private static function request($chList){
        $downloader = curl_multi_init();
        // 将三个待请求对象放入下载器中
        foreach ($chList as $ch){
          curl_multi_add_handle($downloader,$ch);
        }
        $res = array();
        // 轮询
        do {
          while (($execrun = curl_multi_exec($downloader, $running)) == CURLM_CALL_MULTI_PERFORM);
          if ($execrun != CURLM_OK) {
            break;
          }
          // 一旦有一个请求完成,找出来,处理,因为curl底层是select,所以最大受限于1024
          while ($done = curl_multi_info_read($downloader)){
            // 从请求中获取信息、内容、错误
            // $info = curl_getinfo($done['handle']);
            $output = curl_multi_getcontent($done['handle']);
            // $error = curl_error($done['handle']);
            $res[] = $output;
            // 把请求已经完成了得 curl handle 删除
            curl_multi_remove_handle($downloader, $done['handle']);
          }
          // 当没有数据的时候进行堵塞,把 CPU 使用权交出来,避免上面 do 死循环空跑数据导致 CPU 100%
          if ($running) {
            $rel = curl_multi_select($downloader, 1);
            if($rel == -1){
              usleep(1000);
            }
          }
          if($running == false){
            break;
          }
        }while(true);
        curl_multi_close($downloader);
        return $res;
      }
      /**
       * [get description]
       * @param [type] $urlArr
       * @return [type]
       */
      public static function get($urlArr){
        $data = array();
        if (!empty($urlArr)) {
          $chList = array();
          foreach ($urlArr as $key => $url) {
            $chList[] = self::getCurlObject($url);
          }
          $data = self::request($chList);
        }
        return $data;
      }
    }

    相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

    推荐阅读:

    PHP使用file_get_contents发送http请求步骤详解

    PHP通过strace定位排解故障位置并解决

    以上就是使用curl_multi实现并发请求步骤分析的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:multi curl 并发
    上一篇:PHP性能测试工具xhprof实战案例解析 下一篇:使用PHP实现转盘抽奖算法案例解析
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• 整理总结nginx、php-fpm和mysql等的权限划分• 哪位高手有php手册 • 小弟我有关于Linux的有关问题 • 求PHP获取数据表字段值的完整代码解决方法 • 最近有点迷惘,还想请大家指导上学习的方法
    1/1

    PHP中文网