失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > php采集内容中带有图片地址的远程图片并保存办法

php采集内容中带有图片地址的远程图片并保存办法

时间:2023-05-04 12:22:42

相关推荐

php采集内容中带有图片地址的远程图片并保存办法

php教程|php手册

php,采集,内容,中带,图片,地址,远程,图片,并,保存,方法,php,采集,内容,中带,图片,地址,远程

php教程-php手册

php采集内容中带有图片地址的远程图片并保存的方法

微代刷源码,vscode代码结构含义,ubuntu查看分组,tomcat日志文件缺失,爬虫 托运,php soap 实例,seo实战指导pdf下载,jsp简单视频网站源码下载,ppt模板源码网站lzw

这篇文章主要介绍了php采集内容中带有图片地址的远程图片并保存的方法,可实现采集并保存远程图片的功能,是非常实用的技巧,需要的朋友可以参考下

永恒之蓝安卓版源码,华为安卓ubuntu,tomcat配端口号,爬虫技术代理,php中调用分页方法,建瓯seolzw

本文实例讲述了php采集内容中带有图片地址的远程图片并保存的方法。分享给大家供大家参考。具体实现方法如下:

后盾网thinkphp 源码,大家都用ubuntu吗,爬虫怎么爬股票,php woff,seo理论串讲lzw

代码如下:

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;

}

function mkdirs($dir)

{

if(!is_dir($dir)){

if(!mkdirs(dirname($dir))){

return false;}

if(!mkdir($dir,0777)){

return false;}

}

return true;

}

//用法如下:

$str =’fasfsdafsa‘;

echo get_remote($str,’图片’);

如果觉得《php采集内容中带有图片地址的远程图片并保存办法》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。