• PHP curl及并发curl封装函数实现


    单次post

     1 /**
     2  * 发送post请求
     3  *
     4  * @param $array 请求的参数
     5  * @param $timeoutLimit 执行超时时间
     6  * @param $connectTimeoutLimit 连接超时时间
     7  * @return mixed
     8  */
     9 public static function curl($array, $timeoutLimit, $connectTimeoutLimit)
    10 {
    11     $ch = curl_init();
    12     $url = $array['url'];
    13     $isHttps = false;
    14     if(substr($url,0,5)=='https'){//判断是否为https
    15         $isHttps = true;
    16     }
    17     curl_setopt($ch, CURLOPT_PROXY, $array['proxy']);//设置代理
    18     if(strpos(strtolower($array['proxy_type']), 'so') !== false){//socket代理
    19         curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
    20     }else{
    21         curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);//http代理
    22     }
    23     curl_setopt($ch, CURLOPT_URL, $array['url']);//设置目标url
    24     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//返回结果,不输出内容
    25     curl_setopt($ch, CURLOPT_TIMEOUT, $timeoutLimit);
    26     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connectTimeoutLimit);
    27 
    28     //设置header
    29     if (isset($array['header']) && $array['header']) {
    30         curl_setopt($ch, CURLOPT_HEADER, 1);
    31     }
    32     if (isset($array['httpheader'])) {
    33         curl_setopt($ch, CURLOPT_HTTPHEADER, $array['httpheader']);
    34     }
    35     //设置referer
    36     if (isset($array['referer'])) {
    37         curl_setopt($ch, CURLOPT_REFERER, $array['referer']);
    38     }
    39     //是否走post请求
    40     if (isset($array['post'])) {
    41         curl_setopt($ch, CURLOPT_POST, 1);
    42         curl_setopt($ch, CURLOPT_POSTFIELDS, $array['post']);
    43     }
    44     //是否跟踪重定向
    45     if (isset($array['followlocation'])) {
    46         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $array['followlocation']);
    47     }
    48     if (isset($array['cookiefile'])) {//cookie文件
    49         curl_setopt($ch, CURLOPT_COOKIEFILE, $array['cookiefile']);
    50     }
    51     if (isset($array['cookiejar'])) {
    52         curl_setopt($ch, CURLOPT_COOKIEJAR, $array['cookiejar']);
    53     }
    54     if (isset($array['useragent'])) {
    55         curl_setopt($ch, CURLOPT_USERAGENT, $array['useragent']);
    56     }
    57     // 增加https的。不验证证书
    58     if ($isHttps) {
    59         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
    60         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
    61     }
    62     $html = curl_exec($ch);
    63     $r['erro'] = curl_error($ch);
    64     $r['errno'] = curl_errno($ch);
    65     $r['http_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    66     $r['html'] = $html;
    67     curl_close($ch);
    68     return $r;
    69 }
    70 
    71 //样例
    72 //self::curl(['url' => 'https://www.baidu.com'], 1, 1);
    View Code

    并发curl

     1 /**
     2  * 并发curl请求
     3  * 具体每个字段的含义可以参考上面的单个方法
     4  *
     5  * @param $params
     6  * @param $timeoutLimit
     7  * @param $connectTimeoutLimit
     8  */
     9 public static function multiCurl($params, $timeoutLimit, $connectTimeoutLimit)
    10 {
    11     $mch = curl_multi_init();
    12     $connArr = [];
    13     foreach ($params as $key => $val) {
    14         $url = $val['url'];
    15         $connArr[$key] = curl_init($url);
    16         //设置参数 start
    17         if(substr($url,0,5)=='https'){
    18             $isHttps = true;
    19         }
    20         curl_setopt($connArr[$key], CURLOPT_PROXY, $val['proxy']);
    21         if(strpos(strtolower($val['proxy_type']), 'so') !== false){
    22             curl_setopt($connArr[$key], CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
    23         }else{
    24             curl_setopt($connArr[$key], CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
    25         }
    26 //            curl_setopt($connArr[$key], CURLOPT_URL, $val['url']);
    27         curl_setopt($connArr[$key], CURLOPT_RETURNTRANSFER, true);
    28         curl_setopt($connArr[$key], CURLOPT_TIMEOUT, $timeoutLimit);
    29         curl_setopt($connArr[$key], CURLOPT_CONNECTTIMEOUT, $connectTimeoutLimit);
    30 
    31         if (isset($val['header']) && $val['header']) {
    32             curl_setopt($connArr[$key], CURLOPT_HEADER, 1);
    33         }
    34         if (isset($val['httpheader'])) {
    35             curl_setopt($connArr[$key], CURLOPT_HTTPHEADER, $val['httpheader']);
    36         }
    37         if (isset($val['referer'])) {
    38             curl_setopt($connArr[$key], CURLOPT_REFERER, $val['referer']);
    39         }
    40         if (isset($val['post'])) {
    41             curl_setopt($connArr[$key], CURLOPT_POST, 1);
    42             curl_setopt($connArr[$key], CURLOPT_POSTFIELDS, $val['post']);
    43         }
    44         if (isset($val['followlocation'])) {
    45             curl_setopt($connArr[$key], CURLOPT_FOLLOWLOCATION, $val['followlocation']);
    46         }
    47         if (isset($val['cookiefile'])) {
    48             curl_setopt($connArr[$key], CURLOPT_COOKIEFILE, $val['cookiefile']);
    49         }
    50         if (isset($val['cookiejar'])) {
    51             curl_setopt($connArr[$key], CURLOPT_COOKIEJAR, $val['cookiejar']);
    52         }
    53         if (isset($val['useragent'])) {
    54             curl_setopt($connArr[$key], CURLOPT_USERAGENT, $val['useragent']);
    55         }
    56         // 增加https的。
    57         if ($isHttps) {
    58             curl_setopt($connArr[$key], CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
    59             curl_setopt($connArr[$key], CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
    60         }
    61         //设置参数 end
    62         curl_multi_add_handle ($mch, $connArr[$key]); // 添加线程
    63     }
    64 
    65     //执行线程
    66     do {
    67         $mrc = curl_multi_exec($mch,$active);
    68     }while ($mrc == CURLM_CALL_MULTI_PERFORM);
    69 
    70     while ($active and $mrc == CURLM_OK) {
    71         if (curl_multi_select($mch) != -1) {
    72             do {
    73                 $mrc = curl_multi_exec($mch, $active);
    74             } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    75         }
    76     }
    77 
    78     //搜集curl信息
    79     $arrInfo = [];
    80     foreach ($params as $i => $url) {
    81         $arrInfo[$i]['html'] = curl_multi_getcontent($connArr[$i]);
    82         $curlInfo = curl_getinfo($connArr[$i]);
    83         $arrInfo[$i]['total_time'] = $curlInfo['total_time'];
    84         $arrInfo[$i]['http_code'] = $curlInfo['http_code'];
    85         curl_multi_remove_handle($mch, $connArr[$i]);
    86         curl_close($connArr[$i]);
    87     }
    88     curl_multi_close($mch);
    89 
    90     return $arrInfo;
    91 }
    92 
    93 //样例
    94 //self::multiCurl([['url'=>'https://www.baidu.com'], ['url'=>'https://www.google.com']], 1, 1);
    View Code

      

    对应github项目地址:https://github.com/A07110517/PHP_CURL

  • 相关阅读:
    052-141
    052-140
    052-139
    052-138
    需要做笔记的页面
    日期总是显示1900/01/01 0:00:00
    延迟加载的树控件
    (简单)关于summary的注释
    江南检测
    fineui动态添加用户控件
  • 原文地址:https://www.cnblogs.com/asif/p/7993034.html
Copyright © 2020-2023  润新知