curl 扩展默认保持长连接,即没有调用 curl_close(), 连接将保持打开状态。
查看连接数:netstat -an|findstr 80 (for win)
private static $ch;
public static function curlGet($url='', $keepalive=false, $ref='')
{
if (!extension_loaded('curl')) {
exit('need curl extension!');
}
$useragent = 'Mozilla/5.0 (Windows NT 6.1; WOW64)';
$header = [
//
];
try {
if (!self::$ch) {
self::$ch = curl_init();
}
$opts = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $header,
CURLOPT_REFERER => $ref,
CURLOPT_USERAGENT => $useragent,
CURLOPT_TIMEOUT => 10,
];
curl_setopt_array(self::$ch, $opts);
$html = curl_exec(self::$ch);
if (!$keepalive) {
curl_close(self::$ch);
}
return $html;
} catch (\Exception $ex) {
throw $ex;
}
}