加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2023-08-17 12:59:32 所属栏目:PHP教程 来源:
导读:php自动采集内容中带有图片地址的远程图片

function my_file_get_contents($url, $timeout=30) {
if ( function_exists('curl_init') )
{
$ch = curl_init();
curl_setopt ($ch, curlopt_
php自动采集内容中带有图片地址的远程图片

function my_file_get_contents($url, $timeout=30) { 
 if ( function_exists('curl_init') )  
 { 
  $ch = curl_init(); 
  curl_setopt ($ch, curlopt_url, $url); 
  curl_setopt ($ch, curlopt_returntransfer, 1); 
  curl_setopt ($ch, curlopt_connecttimeout, $timeout); 
  $file_contents = curl_exec($ch); 
  curl_close($ch); 
 }  
 else if ( ini_get('allow_url_fopen') == 1 || strtolower(ini_get('allow_url_fopen')) == 'on' )    
 { 
  $file_contents = @file_get_contents($url); 
 }  
 else  
 { 
  $file_contents = ''; 
 } 
 return $file_contents; 

 
 代码如下 复制代码  
function get_remote($body,$title){ 
 
 $img_array = array();  
 $img_path = realpath("../../../upfile/news/").'/'.date("y/m/d/"); //采集远程图片保存地址 
 //die($img_path); 
 $img_rpath='/upfile/news/'.date("y/m/d/");  //设置访问地址 
 $body = stripslashes(strtolower($body));  
 preg_match_all("/(src|src)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|png))/isu",$body,$img_array);  
 $img_array = array_unique($img_array[2]);  
 foreach ($img_array as $key => $value) {  
  $get_file = my_file_get_contents($value,60); 
  $filetime = time();    
  $filename = date("ymdhis",$filetime).rand(1,999).'.'.substr($value,-3,3);  
  if(emptyempty($get_file)){ 
   @sleep(10); 
   $get_file = my_file_get_contents($value,30); 
   if(emptyempty($get_file)){ 
    $body = preg_replace("/".addcslashes($value,"/")."/isu", '/notfound.jpg', $body); 
    continue; 
    } 
  } 
  if(!emptyempty($get_file) ){ 
   if( mkdirs($img_path) ) 
   { 
    $fp = fopen($img_path.$filename,"w"); 
    if(fwrite($fp,$get_file)){          
     $body = preg_replace("/".addcslashes($value,"/")."/isu", $img_rpath.$filename, $body);  
    } 
    fclose($fp); 
    @sleep(6); 
   }    
  }     
  
 } 
 $body =str_replace("<img","<img ",$body);  
 return $body;//开源代码Cuoxin.com 
  

 
function mkdirs($dir) 

 if(!is_dir($dir)){ 
  if(!mkdirs(dirname($dir))){ 
   return false;} 
  if(!mkdir($dir,0777)){ 
   return false;} 
 } 
 return true; 

  
$str ='fasfsdafsa<img src=http://filesimg.Cuoxin.com/2010/03/2010062300391582.jpg />'; 
echo get_remote($str,'图片');
 

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章