关注公众号:青山有路

百度合成语音文件直接上传到七牛

php admin 7年前 (2017-12-27) 1488次浏览 已收录 0个评论 扫描二维码

百度合成语音文件直接上传到七牛

一个小小的urlencode折磨了我一上午

  1. $file = $_SERVER[‘DOCUMENT_ROOT’].’/data/makevoicebaidutoken.txt’;

  2. $modifytime = filemtime($file);

  3. $resp = file_get_contents($file);

  4. $res = json_decode($resp,true);

  5. if(time()>($modifytime+$res[‘expires_in’]-500)){

  6. $resp = file_get_contents(“https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=Glg9WjlKApIOCzNpna9eZWxR&client_secret=Z68xhe8kGzzcgwLhcEwM8332GBK3inmd”);

  7. file_put_contents($file,$resp);

  8. $res = json_decode($resp,true);

  9. }

  10. $access_token = $res[‘access_token’];

  11. $url = “http://tsn.baidu.com/text2audio?tex=”.urlencode($data[‘voicetext’]).”&lan=zh&cuid=00-01-6C-06-A6-29-“.$this->member_id.”&ctp=1&tok=”.$access_token.”&spd=”.$data[‘spd’].”&pit=”.$data[‘pit’].”&vol=”.$data[‘vol’].”&per=”.$data[‘person’];

  12. if($_POST[‘trystr’]==’trystr’){

  13. $this->ajaxreturn(array(‘type’=>”trystr”,”msg”=>”试听”,’url’=>$url));

  14. }

  15. $name = $this->member_id.’_’.rand(1,10000).’_’.date(“YmdHis”).rand(1,10000).’.mp3′;

  16. /* $audiodir = $_SERVER[‘DOCUMENT_ROOT’].’/mp3/’;

  17. $audiofile = $audiodir.$name;

  18. chmod($audiodir,0777);

  19. $resps = file_get_contents($url);

  20. chmod($audiofile,0777);

  21. file_put_contents($audiofile,$resps,true);*/

  22. //直接上传到七牛

  23. // $url = “http://”.$_SERVER[‘HTTP_HOST’].’/mp3/’.$name;

  24. $access_key = Config::AK;

  25. $secret_key = Config::SK;

  26. $fetch = urlsafe_base64_encode($url);

  27. $to = urlsafe_base64_encode(Config::BUCKET_AUDIO_NAME.’:’.$name); //格式为 news2v:123.jpg

  28. $url = ‘http://iovip.qbox.me/fetch/’. $fetch .’/to/’ . $to;

  29. $access_token = generate_access_token($access_key, $secret_key, $url);

  30. $header[] = ‘Content-Type: application/json’;

  31. $header[] = ‘Authorization: QBox ‘. $access_token;

  32. $curl = curl_init(‘iovip.qbox.me/fetch/’.$fetch.’/to/’.$to);

  33. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

  34. curl_setopt($curl, CURLOPT_HEADER,1);

  35. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

  36. curl_setopt($curl, CURLOPT_POST, 1);

  37. curl_setopt($curl, CURLOPT_TIMEOUT,60);

  38. $con = curl_exec($curl);

  39. $res =  APP_AUDIO.’/’.$name;


版权所有丨如未注明 , 均为原创 , 转载请注明百度合成语音文件直接上传到七牛
喜欢 (0)

Warning: Use of undefined constant PRC - assumed 'PRC' (this will throw an Error in a future version of PHP) in /www/wwwroot/gaowenjie.cc/wp-content/themes/Git-alpha/comments.php on line 17
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到