时间:2021-07-01 10:21:17 帮助过:2人阅读
'; $timeStart = microtime(true); function getMoreContent($urls) { $timeout = 30; $rs = array(); $sockets = array(); $userAgent = $_SERVER['HTTP_USER_AGENT']; foreach($urls as $id => $url) { $tmp = parse_url($url); $host = $tmp['host']; $path = isset($tmp['path'])?$tmp['path']:'/'; empty($tmp['query']) or $path .= '?' . $tmp['query']; if (empty($tmp['port'])) { $port = $tmp['scheme'] == 'https'?443:80; } else $port = $tmp['port']; $fp = stream_socket_client("$host:$port", $errno, $errstr, $timeout); if ($fp) { $rs[$id] = ''; $sockets[$id] = $fp; fwrite($fp, "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: $userAgent\r\nConnection: Close\r\n\r\n"); } } // Now, wait for the results to come back in while (count($sockets)) { $read = $sockets; // This is the magic function - explained below if (stream_select($read, $write = null, $e = null, $timeout)) { // readable sockets either have data for us, or are failed connection attempts foreach ($read as $r) { $id = array_search($r, $sockets); $data = fread($r, 8192); if (strlen($data) == 0) { fclose($r); $tmp = explode("\r\n\r\n", $rs[$id], 2); $rs[$id] = strpos($tmp[0], '200')?$tmp[1]:''; unset($sockets[$id]); } else $rs[$id] .= $data; } } } return $rs; } $rs = getMoreContent($urls); foreach($rs as $k => $v) { @file_put_contents($dir . 'b_' . basename($urls[$k]), $v); } $timeEnd = microtime(true); echo sprintf("Spend time: %s second(s)\n", $timeEnd - $timeStart); ?>
$urls = array( 'http://www.jxcms.com/upload/2011/0518/0337319304.jpg', 'http://www.jxcms.com/upload/2011/0310/0951568835.jpg', 'http://www.tzksgs.com/upload/banner1.jpg', 'http://www.tzksgs.com/upload/banner2.jpg' ); $mh = curl_multi_init(); foreach ($urls as $i => $url) { $conn[$i] = curl_init($url); curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER,1); curl_multi_add_handle($mh, $conn[$i]); } do { curl_multi_exec($mh, $active); //在这个循环中,你有机会中断程序的执行。curl_getinfo提供了连接的各种信息 }while($active); foreach ($urls as $i => $url) { $fn = basename($url); file_put_contents($fn, curl_multi_getcontent($conn[$i])); curl_close($conn[$i]); }