php 微信 语音,【微信JSSDK】PHP版微信录音文件下载

转存到自己服务器

调用微信JSSDK API 录音, 录音结束,上传到微信服务器,获取录音文件的 media_id

根据 media_id 下载录音文件(amr)格式

转存到自己服务器(amr需要转码成mp3) 或者 七牛云(有转码功能)

步骤1代码

...

/**

* 开始录音[省略了一部分代码]

*/

startRecord: function() {

var that = this;

if (!that._startRecordFlag) {

typeof wx !== "undefined" && wx.startRecord({

success: function(res) {

Logger.log("res", res)

if (res.errMsg == 'startRecord:ok') {

Logger.log("正在开始录音....")

that._startTime = new Date().getTime();

}

}

});

}

},

/**

* 结束录音,并上传

*/

stopRecord: function() {

that._startRecordFlag = false;

typeof wx !== "undefined" && wx.stopRecord({

success: function(res) {

//上传录音

wx.uploadVoice({

localId: res.localId,

isShowProgressTips: 1,

success: function(resUpload) {

//下载录音文件到服务器,转存起来

Model.downloadRecordAudio(resUpload.serverId, function(result) {

console.log(resUpload.serverId, result.path)

that.attachment = result.path;

// that.attachment = resUpload.serverId;

that.stopRecordCallback && that.stopRecordCallback();

})

}

});

}

});

},

...

步骤2代码

//处理方法,

upload();

//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);

}

//微 信上传下载媒体文件

$url = "http://file.api.weixin.qq.com/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 = "https://api.weixin.qq.com/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);

}

?>

步骤3代码【略】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值