国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php自動采集內(nèi)容中帶有圖片地址的遠(yuǎn)程圖片

2024-05-04 21:48:58
字體:
供稿:網(wǎng)友
  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.  代碼如下 復(fù)制代碼  
  25. function get_remote($body,$title){ 
  26.  
  27.  $img_array = array();  
  28.  $img_path = realpath("../../../upfile/news/").'/'.date("y/m/d/"); //采集遠(yuǎn)程圖片保存地址 
  29.  //die($img_path); 
  30.  $img_rpath='/upfile/news/'.date("y/m/d/");  //設(shè)置訪問地址 
  31.  $body = stripslashes(strtolower($body));  
  32.  preg_match_all("/(src|src)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|png))/isu",$body,$img_array);  
  33.  $img_array = array_unique($img_array[2]);  
  34.  foreach ($img_array as $key => $value) {  
  35.   $get_file = my_file_get_contents($value,60); 
  36.   $filetime = time();    
  37.   $filename = date("ymdhis",$filetime).rand(1,999).'.'.substr($value,-3,3);  
  38.   if(emptyempty($get_file)){ 
  39.    @sleep(10); 
  40.    $get_file = my_file_get_contents($value,30); 
  41.    if(emptyempty($get_file)){ 
  42.     $body = preg_replace("/".addcslashes($value,"/")."/isu"'/notfound.jpg'$body); 
  43.     continue
  44.     } 
  45.   } 
  46.   if(!emptyempty($get_file) ){ 
  47.    if( mkdirs($img_path) ) 
  48.    { 
  49.     $fp = fopen($img_path.$filename,"w"); 
  50.     if(fwrite($fp,$get_file)){          
  51.      $body = preg_replace("/".addcslashes($value,"/")."/isu"$img_rpath.$filename$body);  
  52.     } 
  53.     fclose($fp); 
  54.     @sleep(6); 
  55.    }    
  56.   }     
  57.   
  58.  } 
  59.  $body =str_replace("<img","<img ",$body);  
  60.  return $body;//開源代碼Vevb.com 
  61.   
  62.  
  63. function mkdirs($dir
  64.  if(!is_dir($dir)){ 
  65.   if(!mkdirs(dirname($dir))){ 
  66.    return false;} 
  67.   if(!mkdir($dir,0777)){ 
  68.    return false;} 
  69.  } 
  70.  return true; 
  71.   
  72.  
  73. $str ='fasfsdafsa<img src=http://filesimg.Vevb.com/2010/03/2010062300391582.jpg />'
  74. echo get_remote($str,'圖片');

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 铁力市| 新宾| 眉山市| 德兴市| 固阳县| 乌拉特中旗| 西盟| 望谟县| 丰镇市| 宜城市| 东乡| 前郭尔| 沛县| 博客| 轮台县| 巫山县| 淳安县| SHOW| 宜章县| 武陟县| 洪洞县| 静乐县| 秀山| 柘荣县| 山东| 肥乡县| 清徐县| 鹤岗市| 锦屏县| 沙雅县| 嘉峪关市| 广平县| 澄城县| 化州市| 华坪县| 图木舒克市| 富源县| 屏东市| 新田县| 贵南县| 平凉市|