介绍
这个也是前几天看到的,感觉也挺不错的
主要的功能是 可以通过链接或者远程地址下载文件到本地。
就是说可以下载别人API接口里面的图片、视频、音频保存到本地
(懂的都懂 )
方法
一、使用curl组件,读取图片信息,并存到到本地!
<?php
$url = "API地址"; //远程图片地址
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$file = curl_exec($ch);
curl_close($ch);
$path = 'img/'; //文件存放路径
$resource = fopen($path, 'a');
fwrite($resource, $file);
fclose($resource);
//定时刷新
echo ("<script>setTimeout('window.location.reload()', 1);</script>");
?>
二、使用php中的 file_get_contents 与 file_put_contents 函数
<?php
$url = ''; //远程图片的地址
$file = @file_get_contents($url);
$filend = 'img/'.date('YmdHis').'.png'; //按照日期时间命令文件名
@file_put_contents($filend,$file);
//定时刷新
echo ("<script>setTimeout('window.location.reload()', 1);</script>");
?>
三、注意:在使用下面的方法前,请在php的配置文件php.ini中打开 open_url 设置
<?php
$url = ''; //远程图片的地址
$filename = 'img/'.date("dMYHis").'.png';//文件名称生成
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2 = @fopen($filename, "a");
fwrite($fp2, $img);
fclose($fp2);
//定时刷新
echo ("<script>setTimeout('window.location.reload()', 1);</script>");
?>
四、这个跟上面差不多,可以跳过https请求
<?php
$url = ""; //远程图片地址
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //是否抓取跳转后的页面
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 不从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
curl_setopt($ch, CURLOPT_HEADER ,0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,60);
$file = curl_exec($ch);
curl_close($ch);
$path = 'img/'.date("dMYHis").'.jpg';//文件名称生成
$resource = fopen($path, 'a');
fwrite($resource, $file);
fclose($resource);
echo ("<script>setTimeout('window.location.reload()', 1);</script>");
?>
五、你们也可以加上这个定时刷新已添加了
echo ("<script>setTimeout('window.location.reload()', 1);</script>");
{/card-list-item}
评论 (0)