注意事项:微信这个素材下载接口返回的数据在header头内,这个是坑呀,稍不注意就掉进去了
对接文档–https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_temporary_materials.html
代码示例如下
代码语言:javascript复制记录一下这些都是测试过后可以使用的代码!本博文是记录一下。怎么上传临时的素材后获取id然后进行下载的。在微信里面临时的素材只能保留3天。3天过后便后自动的删除掉了。微信的也不提供获取临时素材的列表。那么看看下面代码
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://img.yuanmabao.com/zijie/pic/2023/10/25/tjzax3uikc3.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();
}
}
?>
未经允许不得转载:肥猫博客 » php 微信开发之新增上传/获取下载临时素材