企业获取临时素材,此处接⼝为语⾳接⼝
1.企业获取临时素材,此处为主要部分代码,加:372319250,咨询企业其他相关问题,或者下载⽂件“企业开发”,查看demo
2.php不同框架⼤同⼩异,⼀定要根据⽂档要求写!
<?php
//media_id为jssdk接⼝上传后返回的媒体id
function upload(){
$media_id = $_POST["media_id"];
$access_token = getAccessToken();
$path = "./weixinrecord/";  //保存路径,相对当前⽂件的路径
$outPath = "./php/weixinrecord/";  //输出路径,给show.php ⽂件⽤,上⼀级
if(!is_dir($path)){
mkdir($path);
getsavefilename
}
//微 信上传下载媒体⽂件
$url = "file.api.weixin.qq/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}";
$filename = "wxupload_".time().rand(1111,9999).".amr";
downAndSaveFile($url,$path."/".$filename);
$data["path"] = $outPath.$filename;
$data["msg"] = "download record audio success!";
// $data["url"] = $url;
echo json_encode($data);
}
//获取Token
function getAccessToken() {
//  access_token 应该全局存储与更新,以下代码以写⼊到⽂件中做⽰例
$data = json_decode(file_get_contents("./access_token.json"));
if ($data->expire_time < time()) {
$appid = "youappid";  //⾃⼰的appid
$appsecret = "youappsecret";  //⾃⼰的appsecret
$url = "api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";        $res = json_decode(httpGet($url));
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen("./access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
}
else {
$access_token = $data->access_token;
}
return $access_token;
}
/
/HTTP get 请求
function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
//根据URL地址,下载⽂件
function downAndSaveFile($url,$savePath){
ob_start();
readfile($url);
$img  = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp = fopen($savePath, 'a');
fwrite($fp, $img);
fclose($fp);
}
>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。