This article mainly introduces php to capture images, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
function actionGetimg(){ ob_end_clean(); ob_implicit_flush(1); //13050 for($a=1;$a<13074;$a++){ echo $a.'+'; echo '<br/>'; $url = "http://www.jtyxzz.com/portal.php?mod=view&aid=".$a; $arr = $this->file_from_url_content($url); $regex4 = "/<img src=\"data\/[\s\S]*?\">/";//标题 preg_match_all($regex4, $arr, $result1); //var_dump($result1); //var_dump(empty($result1[0]));exit; if (!empty($result1[0])) { foreach ($result1 as $key => $val) { $first = str_replace('<img src="', '', $val); $second = str_replace('">', '', $first); } //dump($second);exit; foreach ($second as $key => $val) { //var_dump($val); //echo '<br/>'; $zuihou=strrpos($val,'/'); $mulu=substr($val,0,$zuihou); //$ming=substr($val,$zuihou+1); //dump($ming);exit; if(is_dir($mulu)){ //echo 'cunzai'; }else{//目录不存在创建目录 //echo 'bucunzai';exit; mkdir($mulu,0777,true); } $image =@file_get_contents('http://www.jtyxzz.com/' . $val); if($image){ file_put_contents($val, $image); //echo $val;exit; }else{ file_put_contents('images/diushi.txt',$a.'/'.$val.'图片丢失<br/>',FILE_APPEND); echo $a.'/'.$val.'图片丢失'; echo '<br/>'; } } }else { echo $a.'no img or no permission'; echo '<br/>'; } $sleep=$a/100; if(is_int($sleep)){ echo $sleep; echo '<br/>'; echo $a; sleep(rand(40,50)); } } echo '图片采集完毕'; //var_dump($second);exit; exit; } /** * 异步将远程链接上的内容 * @param unknown $url 远程地址 * @param unknown $saveName 保存在服务器上的文件名 * @param unknown $path 保存路径 * @return boolean */ function file_from_url_content($url) { // 设置运行时间为无限制 set_time_limit ( 0 ); $url = trim ( $url ); $curl = curl_init (); // 设置你需要抓取的URL curl_setopt ( $curl, CURLOPT_URL, $url ); // 设置header curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。 curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 运行cURL,请求网页 $file = curl_exec ( $curl ); // 关闭URL请求 curl_close ( $curl ); return $file; }
Related recommendations:
Example of method of using PHP to capture Baidu Reading
php capture page garbled analysis
The above is the detailed content of php capture pictures. For more information, please follow other related articles on the PHP Chinese website!