--------------------------------------------基础不好先做上篇单线程,简单一点----------------------------------------------
1、抓取图片链接
$file_contents = file_get_contents("http://beta.microdemo.soarsvr.com/"); preg_match_all('/w+://[w.]+/w+/w+/[w.]+(.jpg|.png)/',$file_contents, $pics);
仍然可以自己写一个图片链接的数组进行测试
2、设置抓取时间(可以略过,页面显示时查看方便)
$pics = $pics[0]; $beginTime = time(); $lastTime = $beginTime; $count = 0; echo 'begin download at ' . date('Y-m-d H:i:s', $beginTime) . '<br/>'; set_time_limit(300); echo date('H:i:s',time())."<br/><hr>"; $timeout = 5;
3、抓取图片信息
$mh = curl_multi_init(); #返回url批处理的句柄 foreach ($pics as $k => $v) { $ch[$k] = curl_init(); curl_setopt($ch[$k], CURLOPT_URL, $v); curl_setopt($ch[$k], CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch[$k], CURLOPT_HEADER, 0); curl_multi_add_handle($mh, $ch[$k]); #向curl批处理会话中添加单独句柄 } do { $status = curl_multi_exec($mh, $active); #处理在栈中的每一个句柄 $result = curl_multi_info_read($mh); #获取当前解析的cURL的相关传输信息 if ($result !== false) { $content = curl_multi_getcontent($result['handle']); #那么这个函数将会以字符串的形式返回那个cURL句柄获取的内容。 $picName = substr($pics[$count], strrpos($pics[$count], '/')+1); $savePath = './multi/'; if (!is_dir($savePath)) { @mkdir($savePath, 0777); } $saveName = $savePath . $picName; $fp = fopen($saveName, 'w'); fwrite($fp, $content); fclose($fp); $nowTime = time(); $takeTime = $nowTime - $lastTime; ++$count; echo 'downloaded ' . $count . 'th picture take time ' . $takeTime . 's<br/>'; $lastTime = $nowTime; } } while ($status == CURLM_CALL_MULTI_PERFORM || $active); curl_multi_close($mh);
4、设置结束时间
$endTime = time(); $totalTime = $endTime - $beginTime; echo '<hr>end download at ' . date('Y-m-d H:i:s', $endTime) . '<br/>'; echo 'downloaded ' . $count . ' pictures take time ' . $totalTime . ' s<br/>';
5、进入目录中查看图片。