蓝优小窝
蓝优
始于网络,终于现实
90后 狮子座 联系
代码是网上找来的,然后略做修改。
参数自行琢磨,只是举个简单例子

multi并发请求例子

$param = array(
    array("url"=>"https://api.moxu.cc/api.html?action=haoma&text=1410469560"),
    array("url"=>"https://api.moxu.cc/api.html?action=haoma&text=1410469560")
);
$result = Curl::Request($param,"multi");
var_dump($result);
//文本流
$param = array(
    array("url"=>"https://www.baidu.com/"),
    array("url"=>"https://www.baidu.com/")
);
$result = Curl::Request($param,"param","html");
echo $result;

单例请求例子

//默认返回格式json
$param = array('url'=>"https://api.moxu.cc/api.html?action=haoma&text=1410469560");
$result = Curl::Request($param);
var_dump($result);
//文本流
$param = array('url'=>"https://www.baidu.com/");
$result = Curl::Request($param,"single","html");
echo $result;

Curl类

/**
 * CURL高并发请求
 */

class Curl
{
    /**
	 * @param {类别} $type single单例请求  multi多个请求
	 * @param {参数} $params
	 * @param {返回格式} $format  json/文本流
	 */
    public static function Request($params,$type="single",$format="json")
    {
		if ($type == "single") {
			$array = array($params);
			$request = self::multi($array);
			$result = $format == "json" ? json_decode($request[0]["html"],true) : $request[0]["html"];
		}elseif ($type == "multi") {
			$request = self::multi($params);
			foreach ($request as $row) {
				$data[] = $format == "json" ? json_decode($row["html"],true) : $row["html"];
			}
			$result = $data;
		}
        return $result;
    }
    public static function multi($params, $timeoutLimit=10, $connectTimeoutLimit=1)
    {
        $mch = curl_multi_init();
        $connArr = [];
        foreach ($params as $key => $val) {
            $url = $val['url'];
            $connArr[$key] = curl_init($url);
            //设置参数 start
            if (substr($url, 0, 5)=='https') {
                $isHttps = true;
            }
            curl_setopt($connArr[$key], CURLOPT_PROXY, $val['proxy']);
            if (strpos(strtolower($val['proxy_type']), 'so') !== false) {
                curl_setopt($connArr[$key], CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
            } else {
                curl_setopt($connArr[$key], CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
            }
            //curl_setopt($connArr[$key], CURLOPT_URL, $val['url']);
            curl_setopt($connArr[$key], CURLOPT_RETURNTRANSFER, true);
            curl_setopt($connArr[$key], CURLOPT_TIMEOUT, $timeoutLimit);
            curl_setopt($connArr[$key], CURLOPT_CONNECTTIMEOUT, $connectTimeoutLimit);
            if (isset($val['header']) && $val['header']) {
                curl_setopt($connArr[$key], CURLOPT_HEADER, 1);
            }
            if (isset($val['httpheader'])) {
                curl_setopt($connArr[$key], CURLOPT_HTTPHEADER, $val['httpheader']);
            }
            if (isset($val['referer'])) {
                curl_setopt($connArr[$key], CURLOPT_REFERER, $val['referer']);
            }
            if (isset($val['post'])) {
                curl_setopt($connArr[$key], CURLOPT_POST, 1);
                curl_setopt($connArr[$key], CURLOPT_POSTFIELDS, $val['post']);
            }
            if (isset($val['followlocation'])) {
                curl_setopt($connArr[$key], CURLOPT_FOLLOWLOCATION, $val['followlocation']);
            }
            if (isset($val['cookiefile'])) {
                curl_setopt($connArr[$key], CURLOPT_COOKIEFILE, $val['cookiefile']);
            }
            if (isset($val['cookiejar'])) {
                curl_setopt($connArr[$key], CURLOPT_COOKIEJAR, $val['cookiejar']);
            }
            if (isset($val['useragent'])) {
                curl_setopt($connArr[$key], CURLOPT_USERAGENT, $val['useragent']);
            }
            // 增加https的。
            if ($isHttps) {
                curl_setopt($connArr[$key], CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
				curl_setopt($connArr[$key], CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
            }
            curl_setopt($connArr[$key], CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
            //设置参数 end
			curl_multi_add_handle($mch, $connArr[$key]); // 添加线程
        }
        //执行线程
        do {
            $mrc = curl_multi_exec($mch, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        while ($active and $mrc == CURLM_OK) {
            if (curl_multi_select($mch) != -1) {
                do {
                    $mrc = curl_multi_exec($mch, $active);
                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
            }
        }
        //搜集curl信息
        $arrInfo = [];
        foreach ($params as $i => $url) {
            $arrInfo[$i]['html'] = curl_multi_getcontent($connArr[$i]);
            $curlInfo = curl_getinfo($connArr[$i]);
            $arrInfo[$i]['total_time'] = $curlInfo['total_time'];
            $arrInfo[$i]['http_code'] = $curlInfo['http_code'];
            curl_multi_remove_handle($mch, $connArr[$i]);
            curl_close($connArr[$i]);
        }
        curl_multi_close($mch);
        $result = $arrInfo;
        return $result;
    }
}
声明:若无特殊注明,本文为《蓝优》原创,转载请保留文章出处。
  1. 神秘网友
    slwtde89035SL-支持多种语言,不仅有中文文章,还有很多外文资讯,可以扩展视野。http://wap.ytld.xyz/
  2. 哈哈哈
    不错