六九博客-分享技术那些事六九博客-分享技术那些事

本博客仅做技术交流,
操作者不可用于违法用途!

PHPcurl提交源码

免责声明:
本站资源并不针对任何人,任何机构,任何网站。
本站资源仅用于个人学习研究,请勿用于非法用途。
若因使用本站资源引起各类民事,刑事纠纷,属使用者的个人行为,与本站资源作者无关。
从你开始使用本服务,默认接受使用条款。
/**
 * curl 多线程
 * @param array $array 并行网址
 * @param int $timeout 超时时间
 * @return array
 */
function curl_http($urls, $header=array(), $timeout=5, $ip=array()){
    $startTime = microtime(true);

    $UserAgent = array(
        'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)',
        'Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; c8650 Build/GWK74) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/4.5 Mobile Safari/533.1s',
        'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.3226.400 QQBrowser/9.6.11682.400'
    );

    $mh = curl_multi_init();//创建多个curl语柄
    foreach($urls as $k => $url){

        $conn[$k] = curl_init($url['url']);
        curl_setopt($conn[$k], CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($conn[$k], CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($conn[$k], CURLOPT_RETURNTRANSFER, 1);
        
        //设置代理
        curl_setopt($conn[$k], CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
        curl_setopt($conn[$k], CURLOPT_PROXY, "{$url['ip']}:{$url['port']}");
        //设置代理用户名密码(私密代理/独享代理)
        // if(!empty($ip['user'])){
        //     curl_setopt($conn[$k], CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
        //     curl_setopt($conn[$k], CURLOPT_PROXYUSERPWD, "{$ip['user']}:{$ip['passwd']}");
        // }

        // curl_setopt($conn[$k], CURLOPT_USERAGENT, $UserAgent[rand(0,count($UserAgent) - 1)]);

        // curl_setopt($conn[$k], CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //强制协议为1.0

        // curl_setopt($conn[$k], CURLOPT_HTTPHEADER, array('Expect: ')); //头部要送出'Expect: '

        // curl_setopt($conn[$k], CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); //强制使用IPV4协议解析域名

        // if(isset($header['Referer'])){
        //     curl_setopt($conn[$k], CURLOPT_REFERER, $header['Referer']);
        // }

        curl_setopt($conn[$k], CURLOPT_URL, $url['url']);
        curl_setopt($conn[$k], CURLOPT_HTTPHEADER, $header);
        if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
            curl_setopt($conn[$k], CURLOPT_FOLLOWLOCATION, true);
        }

        curl_setopt($conn[$k], CURLOPT_ENCODING, 'gzip'); //使用gzip压缩传输数据让访问更快
        curl_setopt($conn[$k], CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($conn[$k], CURLOPT_TIMEOUT, $timeout);

        curl_setopt($conn[$k], CURLOPT_HEADER, 0);//这里不要header,加块效率
        curl_multi_add_handle ($mh, $conn[$k]);
    }

    // 执行批处理句柄
    $active = null;
    do{
        $mrc = curl_multi_exec($mh,$active);//当无数据,active=true
    }while($mrc == CURLM_CALL_MULTI_PERFORM);//当正在接受数据时

    while($active && $mrc == CURLM_OK){//当无数据时或请求暂停时,active=true
        // if(curl_multi_select($mh) != -1){
        do{
            $mrc = curl_multi_exec($mh, $active);
        }while($mrc == CURLM_CALL_MULTI_PERFORM);
        // }
    }

    $res = array();
    foreach ($urls as $k => $url) {
        curl_error($conn[$k]);
        $body = curl_multi_getcontent($conn[$k]);//获得返回信息
        $curl_getinfo = curl_getinfo($conn[$k]);//返回头信息

        if(isset($curl_getinfo['total_time'])){
            $curl_getinfo['total_time'] = intval(round($curl_getinfo['total_time'] * 1000));
        }
        if(isset($curl_getinfo['namelookup_time'])){
            $curl_getinfo['namelookup_time'] = intval(round($curl_getinfo['namelookup_time'] * 1000));
        }
        if(isset($curl_getinfo['connect_time'])){
            $curl_getinfo['connect_time'] = intval(round($curl_getinfo['connect_time'] * 1000));
        }
        if(isset($curl_getinfo['pretransfer_time'])){
            $curl_getinfo['pretransfer_time'] = intval(round($curl_getinfo['pretransfer_time'] * 1000));
        }
        if(isset($curl_getinfo['speed_download'])){
            $curl_getinfo['speed_download'] = intval($curl_getinfo['speed_download']);
        }
        
        $data[0] = $url['ip'];
        $data[1] = $url['port'];
        $data[2] = $body;
        $data[3] = $curl_getinfo;     // consume
        $data[4] = $check_type;     // support
        $data[5] = 0;   // proxy_type
        if(!empty($body)){
            $anonymity = json_decode($body, true);
            $data[5] = intval($anonymity['proxy_type']);
        }
        $res[$k] = $data;

        curl_close($conn[$k]);//关闭语柄
        // if($conn[$k]){
        //     curl_multi_remove_handle($mh, $conn[$k]);//释放资源
        // }
    }

    curl_multi_close($mh);
    return $res;
}
未经允许不得转载:六九博客-分享技术那些事 » PHPcurl提交源码
添加新评论