PHP development-temporary recording to permanent storage, php recording storage
This problem was encountered during recent development. Party A hopes that the user's on-End recording will always be valid. That is, you can open a friend's share at any time to hear a friend's recording. You can find in the Official development manual that the recording file can only be retained for three days on the end after being uploaded, and will expire in three days. What should we do?
1. Call the media resource download API to download the recording to a local device.
@ ParamsString access_token: access_tokenString serverId serverIdhttp: // file.api.weixin.qq.com/cgi-bin/media/get? Access_token = {$ access_token} & media_id = {$ serverId}
2. The format of the recording file downloaded from the local device is amr, while the webpage browser does not support playing the audio file of the amr file. So we need to convert amr
Mp3 formats that can be recognized by webpages.
How to convert it? I have found a platform that supports this conversion. The qiniu cloud storage platform can convert amr to mp3 through qiniu cloud storage.
Require (". /extension/autoload. php "); // The official development kit can download the php version to load its autoload. php extension is a random name/*** @ param * String $ filepa local amr file path * @ return * String the mp3 remote address stored in qiniu bucket after parsing */ function upchange ($ filepa) {$ accessKey = "qiniu accessKey obtained in qiniu backend"; $ secretKey = "qiniu secretKey obtained in qiniu backend "; $ httpUrl = "access addresses allocated by qiniu are obtained in qiniu backend"; $ bucket = "bucket object bucket created in qiniu backend "; // queue name used for transcoding $ pipeline = 'create a transcoding queue name in Qiniu backend '; $ auth = new \ Qiniu \ Au Th ($ accessKey, $ secretKey); $ rand_name = getrandchar((62.16.time '; // transcoding operations to be performed $ fops = "avthumb/mp3/AB/64 k/ar/22050"; // $ fops = "mp3 "; // you can use the saveas parameter to customize the name of the transcoded file. Of course, if you do not specify a file, it will be named by default and saved in the room $ savekey = Qiniu \ base64_urlSafeEncode ('kepei: '. $ rand_name); $ fops = $ fops. '| saveas /'. $ savekey; $ policy = array ('persistentops '=> $ fops, 'persistentpolicyurl' => 'HTTP: //',); $ uptoken = $ auth-> uploadToken ($ Bucket, null, 3600, $ policy); // local path of the uploaded file $ filePath = $ filepa; $ key = $ rand_name; // $ key = ''; $ uploadMgr = new \ Qiniu \ Storage \ UploadManager (); list ($ ret, $ err) = $ uploadMgr-> putFile ($ uptoken, $ key, $ filePath ); if ($ err! = Null) {return false;} else {return $ httpUrl. $ rand_name ;}} function getRandChar _ ($ length) {$ str = null; $ strPol = "comment"; $ max = strlen ($ strPol)-1; for ($ I = 0; $ I <$ length; $ I ++) {$ str. = $ strPol [rand (0, $ max)]; // rand ($ min, $ max) generate a random integer between min and max} return $ str ;}$ amrToMp3 = upchange ('local amr file path '); // The access address var_dump ($ amrToMp3) of mp3 on qiniu;/*** if you want to store this mp3 on your local host rather than on qiniu, then you need to wait for several seconds in the program, because qiniu is converting the amr format */sleep (10); $ res = curlHttp ($ amrToMp3 ); // curlHttp is the php curl mechanism. If you do not understand it, you can refer to my following article mechanism $ open = fopen('demo', 'w'); fwrite ($ open, $ res ); fclose ($ open );
Php curlHttp
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.