PHP 微信公众号临时素材创建教程
以下是使用PHP创建微信临时素材的示例代码:
// 替换为自己的appid和appsecret
$appid = 'your_appid';
$appsecret = 'your_appsecret';
// 获取access_token
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
$res = json_decode(file_get_contents($url));
$access_token = $res->access_token;
// 上传临时素材
$type = 'image'; // 素材类型,可选值:image、voice、video、thumb
$file = '/path/to/your/file'; // 文件路径
$url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.$access_token.'&type='.$type;
$data = array('media' => new CURLFile(realpath($file)));
$res = json_decode(http_post($url, $data));
$media_id = $res->media_id;
// 输出结果
echo 'media_id: '.$media_id;
// 发送POST请求
function http_post($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
注意:在使用该代码时,需要将'your_appid'和'your_appsecret'替换为自己的实际值,将'/path/to/your/file'替换为要上传的文件路径。另外,需要确保服务器支持'CURL'扩展。
原文地址: https://www.cveoy.top/t/topic/l3xe 著作权归作者所有。请勿转载和采集!