php 微信开发之新增上传/获取下载临时素材
php 微信开发之新增上传/获取下载临时素材 代码
<?php
define("AppID","");//你的id
define("AppSecret", "");//你的secret
/* 上传临时文件 */
$a = "0";
if($a == "1"){
$type = "image";
$filepath = dirname(__FILE__)."\w.jpg";
$filedata = array("file1" => "@".$filepath);
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".token()."&type=".$type;
$result = https_request($url, $filedata);
$p = json_decode($result);
echo "media_id:".$p->media_id;
}
/* 获取临时的文件 */
$b = "1";
if($b == "1"){
$id = "Zary6julqwRBBuSgzFbiMCSjYmG2930UvzjrbnHN4nyT3YGZVD8H-ecfoReGT1Qr";
$url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=".token()."&media_id=".$id;
$arr = downloadWeixinFile($url);
saveWeixinFile("1.jpg",$arr['body']);
}
function downloadWeixinFile($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0); //只取body头
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$package = curl_exec($ch);
$httpinfo = curl_getinfo($ch);
curl_close($ch);
$imageAll = array_merge(array('header' => $httpinfo), array('body' => $package));
return $imageAll;
}
function saveWeixinFile($filename, $filecontent)
{
$local_file = fopen($filename, 'w');
if (false !== $local_file){
if (false !== fwrite($local_file, $filecontent)) {
fclose($local_file);
}
}
}
function token(){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".AppID."&secret=".AppSecret;
$data = json_decode(file_get_contents($url),true);
if($data['access_token']){
return $data['access_token'];
}else{
echo "Error";
exit();
}
}
?>
使用的注意事情。文件必须需要绝对的路径不能相对路径然后在路径前面加个@ (列子: @F:\\w.jpg) 这样才是正确的 然后type 类型自己定义一下。上传视频的为 video 图片为 image 音乐为 voice
图片(image): 1M,支持JPG格式
语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式
视频(video):10MB,支持MP4格式
缩略图(thumb):64KB,支持JPG格式
各位记住了