Use PHP to download files from WeChat server to local server

巴扎黑
Release: 2016-11-09 13:31:01
Original
2311 people have browsed it

As we all know, after uploading the file to WeChat, use $src= "https://api.weixin.qq.com/cgi-bin/media/get?access_token=" . $access_token . "&media_id=" . $f ['file']; can get the file information. If it is a picture, use the img tag to set its src attribute to $src to display the picture on the page.

This article uses this as the URL of the file to download the file:

$url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=" . $access_token . "&media_id=" . $f ['file'];

where $access_token is the signature obtained after encrypting the APPID and secret key registered on the WeChat public platform, $f ['file'] is the signature after uploading to the WeChat server The WeChat information of the returned file on the server is a string, somewhat similar to: After the file is uploaded to the server, a piece of data is inserted into the file table to record the basic information such as the name, path, and AppId of the file, and this is The primary key sequence number of the piece of data is returned to the client. $f ['file'] is the primary key sequence number.

Call method for file download:

var $array = $this->getImage($url,$path,$fileName,1);

Specific code for file download:

/*
*功能:下载远程图片保存到本地
*参数:
*$url:需要下载的文件url,
*$path:下载下来的文件需要保存到的目录
*$fileName:保存文件名称,当保存文件名称为空时则使用远程文件原来的名称
*type:使用的下载方式
*/
function getImage($url,$save_dir='',$filename='',$type=0){
$ext=".jpg";//以jpg的格式结尾
clearstatcache();//清除文件缓存
if(trim($url)==''){
return array('file_name'=>'','save_path'=>'','error'=>1);
}
if(trim($save_dir)==''){
$save_dir='./';
}
if(trim($filename)==''){//保存文件名
$filename=time().$ext;
}else{
$filename = $filename.$ext;
}
if(0!==strrpos($save_dir,'/')){
$save_dir.='/';
}
//创建保存目录
if(!is_dir($save_dir)){//文件夹不存在,则新建
//print_r($save_dir."文件不存在");
mkdir(iconv("UTF-8", "GBK", $save_dir),0777,true);
//mkdir($save_dir,0777,true);
}
//获取远程文件所采用的方法 
if($type){
$ch=curl_init();
$timeout=3;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$img=curl_exec($ch);
curl_close($ch);
}else{
ob_start(); 
readfile($url);
$img=ob_get_contents(); 
ob_end_clean(); 
}
$size=strlen($img);
//文件大小 
//var_dump("文件大小:".$size);
$fp2=@fopen($save_dir.$filename,'w');
fwrite($fp2,$img);
fclose($fp2);
unset($img,$url);
return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0);
}
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template