我相信 lz 是想做伸手党的,分享一个我前年写的微信 v1.0 的代码。。。
/**
* Wechat::fileGet()
* 拉取媒体文件
* @
param string $accessToken
* @
param string $mediaId
* @
return */
public function fileGet($accessToken, $mediaId){
$url = '
http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=' . $accessToken . '&media_id=' . $mediaId;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0); //对 body 进行输出。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_SSLVERSION, 1); //
https://mp.weixin.qq.com/cgi-bin/announce?action=getannouncement&key=1414562353&version=12&lang=zh_CN , 1 ==> CURL_SSLVERSION_TLSv1
$package = curl_exec($ch);
$httpinfo = curl_getinfo($ch);
curl_close($ch);
$media = array_merge(array('mediaBody' => $package), $httpinfo);
//log_message('debug', var_export($media, true));
//求出文件格式
preg_match('/\w\/(\w+)/i', $media["content_type"], $extmatches);
$fileExt = $extmatches[1];
$filename = time().rand(100,999).".{$fileExt}";
$date = date('Ym');
$dirname = 'uploads/' . $date . '/';
$localdir = 'D:\\ykwxggzh\\wechat\\uploads\\' . $date . '\\';
if(!file_exists($localdir)){
mkdir($localdir,0777,true) ? null : mkdir($localdir);
}
file_put_contents($localdir.$filename,$media['mediaBody']);
return $dirname.$filename;
}