curl 擴展默認保持長鏈接,即沒有調用 curl_close(), 鏈接將保持打開狀態。php
查看鏈接數:netstat -an|findstr 80 (for win)html
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; } }
http://stackoverflow.com/questions/972925/persistent-keepalive-http-with-the-php-curl-librarycurl