当前位置:首页 > PHP教程 > php高级应用 > 列表

php自动采集内容中带有图片地址的远程图片

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-09 15:28:52 浏览: 评论:0 
  1. function my_file_get_contents($url, $timeout=30) { 
  2.  if ( function_exists('curl_init') )  
  3.  { 
  4.   $ch = curl_init(); 
  5.   curl_setopt ($ch, curlopt_url, $url); 
  6.   curl_setopt ($ch, curlopt_returntransfer, 1); 
  7.   curl_setopt ($ch, curlopt_connecttimeout, $timeout); 
  8.   $file_contents = curl_exec($ch); 
  9.   curl_close($ch); 
  10.  }  
  11.  else if ( ini_get('allow_url_fopen') == 1 || strtolower(ini_get('allow_url_fopen')) == 'on' )    
  12.  { 
  13.   $file_contents = @file_get_contents($url); 
  14.  }  
  15.  else  
  16.  { 
  17.   $file_contents = ''; 
  18.  } 
  19.  return $file_contents; 
  20. } 
  21.  
  22.  
  23.   
  24.   
  25.  代码如下 复制代码  
  26. function get_remote($body,$title){ 
  27.  
  28.  $img_array = array();  
  29.  $img_path = realpath("../../../upfile/news/").'/'.date("y/m/d/"); //采集远程图片保存地址 
  30.  //die($img_path); 
  31.  $img_rpath='/upfile/news/'.date("y/m/d/");  //设置访问地址 
  32.  $body = stripslashes(strtolower($body));  
  33.  preg_match_all("/(src|src)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|png))/isu",$body,$img_array);  
  34.  $img_array = array_unique($img_array[2]);  
  35.  foreach ($img_array as $key => $value) {  
  36.   $get_file = my_file_get_contents($value,60); 
  37.   $filetime = time();    
  38.   $filename = date("ymdhis",$filetime).rand(1,999).'.'.substr($value,-3,3);  
  39.   if(emptyempty($get_file)){ 
  40.    @sleep(10); 
  41.    $get_file = my_file_get_contents($value,30); 
  42.    if(emptyempty($get_file)){ 
  43.     $body = preg_replace("/".addcslashes($value,"/")."/isu", '/notfound.jpg', $body); 
  44.     continue; 
  45.     } 
  46.   } 
  47.   if(!emptyempty($get_file) ){ 
  48.    if( mkdirs($img_path) ) 
  49.    { 
  50.     $fp = fopen($img_path.$filename,"w"); 
  51.     if(fwrite($fp,$get_file)){          
  52.      $body = preg_replace("/".addcslashes($value,"/")."/isu", $img_rpath.$filename, $body);  
  53.     } 
  54.     fclose($fp); 
  55.     @sleep(6); 
  56.    }    
  57.   }     
  58.   
  59.  } 
  60.  $body =str_replace("<img","<img ",$body);  
  61.  return $body;//开源代码phpfensi.com 
  62.   
  63. } 
  64.  
  65. function mkdirs($dir) 
  66. { 
  67.  if(!is_dir($dir)){ 
  68.   if(!mkdirs(dirname($dir))){ 
  69.    return false;} 
  70.   if(!mkdir($dir,0777)){ 
  71.    return false;} 
  72.  } 
  73.  return true; 
  74. } 
  75.   
  76.  
  77. $str ='fasfsdafsa<img src=http://filesimg.phpfensi.com/2010/03/2010062300391582.jpg />'; 
  78. echo get_remote($str,'图片');

Tags: php自动采集 php远程图片

分享到: