流程:
本地指定临时或永久素材后调用微信媒体素材上传api,将文件上传到微信服务器,微信返回含media_id的信息,本地数据库存入相关信息,以供以后素材使用。
代码
素材上传页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>素材管理</title>
</head>
<body>
<form action="upSave.php" method="post" enctype="multipart/form-data">
<p>
素材类型:
<select name="is_forever">
<option value="0">零时</option>
<option value="1">永久</option>
</select>
</p>
<p>
<input type="file" name="media" id="">
</p>
<p>
<input type="submit" value="提交素材">
</p>
</form>
</body>
</html>
<!-- 临时 https https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE -->
<!-- 永久 https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN -->
素材上传接口
<?php
// echo '<pre>';
// print_r($_POST);
// print_r($_FILES);
// 引入数据库
$pdo = include './db.php';
// 引入微信操作类
include './wxz.php';
$files = $_FILES['pic'];
# 拓展名 无点
$ext = pathinfo($files['name'],PATHINFO_EXTENSION);
# 上传后的文件名
$name = time().'.'.$ext;
// 上传到服务器绝对路径
$realPath = __DIR__.'/up/'.$name;
move_uploaded_file($files['tmp_name'],$realPath);
$sql = "insert into material (realpath, ctime, is_forever, media_id) values (?,?,?,?)";
// 预处理对象
$stmt = $pdo->prepare($sql);
// 上传素材到公众平台
$wx = new Wxz();
$media_id = $wx->uploadM($realPath,'image',$_POST['is_forever']);
// 执行
$ret = $stmt->execute([$realPath,time(),intval($_POST['is_forever']),$media_id]);
db.php 与 wxz.php 见
https://blog.csdn.net/weixin_42043407/article/details/116905422
https://blog.csdn.net/weixin_42043407/article/details/116905455