时间:2022-12-26加入收藏
<?php
// 返回JSON
header("Content-type:application/json");
// 容许上传的图片后缀
$allowedExts = array("jpeg", "jpg", "png");
// 后缀名
if ($allowedExts[0] == 'jpeg') {
$hzm = 'jpg';
}else{
$hzm = $allowedExts[0];
}
// 获取抉择的文件
$temp = explode(".", $_FILES["file"]["name"]);
// 获取文件后缀名
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 10485760)
&& in_array($extension, $allowedExts)){
if ($_FILES["file"]["error"] > 0){
$result = array(
'code' => 201,
'msg' => '上传失败'.$_FILES["file"]["error"]
);
}else{
// 重命名
$new_file = date("Y-m-d")."-".rand(10000,99999).".".$hzm;
// 上传到本人的服务器
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$new_file);
// 获取实在地址
$filepath = realpath(dirname(__FILE__));
$upload_filepath = $filepath."/upload/".$new_file;
// 上传到微信服务器
$imgurl = upload_img($upload_filepath);
// 验证上传后果
if(strpos($imgurl,'http') !== false){
// 上传胜利
$result = array(
'code' => 200,
'msg' => '上传胜利',
'url' => $imgurl
);
// 删除本地素材
unlink($upload_filepath);
}else{
// 上传失败
$result = array(
'code' => 202,
'msg' => '上传失败'
);
}
}
}
// 获取access_token
function getToken(){
// appid和secret
$appid='填写你公众号的appid';
$appsecret='填写你公众号的appsecret';
// 读取access_token
include './access_token.php';
// 判断是否过期
if (time() > $access_token['expires']){
// 如果曾经过期就得从新获取并缓存
$access_token = array();
$access_token['access_token'] = getNewToken($appid,$appsecret);
$access_token['expires']=time()+7000;
// 将数组写入php文件
$arr = '<?php'.PHP_EOL.'$access_token = '.var_export($access_token,true).';'.PHP_EOL.'?>';
$arrfile = fopen("./access_token.php","w");
fwrite($arrfile,$arr);
fclose($arrfile);
// 返回以后的access_token
return $access_token['access_token'];
}else{
// 如果没有过期就间接读取缓存文件
return $access_token['access_token'];
}
}
// 获取新的access_token
function getNewToken($appid,$appsecret){
$url = "https://<a href="https://www.bingdou.com.cn" title="查看更多文章" target="_blank">api</a>.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
$access_token_Arr = https_request($url);
return $access_token_Arr['access_token'];
}
// curl申请函数
function https_request ($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$out = curl_exec($ch);
curl_close($ch);
return json_decode($out,true);
}
// 上传图片素材到微信服务器
function upload_img($realpath){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/material/add_material?access_token='.getToken().'&type=image');
$data = array(
'media' => new CURLFile($realpath)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$upimg = curl_exec($ch);
return json_decode($upimg)->url;
curl_close($ch);
}
// 返回JSON对象
echo json_encode($result,JSON_UNESCAPED_UNICODE);
?>
{"media_id":"8IZhZUPXxsG_omeA5giO5By8VyHnjk7_oy0Co9jVWwxpgm-sqhx_Hkz_9rLVF9Ws","url":"http:\/\/mmbiz.qpic.cn\/mmbiz_png\/5zLSKyuEW2Kt5ZGZg7XUx05QyGOVFCpHqKic74qQP4gxzQJYXjwN4aGEiadtfUXax4fCXXV5ia1UnvSwdqxuqLCqA\/0?wx_fmt=png","item":[]}
TGA: 技巧