百度合成语音文件直接上传到七牛
一个小小的urlencode折磨了我一上午
-
$file = $_SERVER[‘DOCUMENT_ROOT’].’/data/makevoicebaidutoken.txt’;
-
$modifytime = filemtime($file);
-
$resp = file_get_contents($file);
-
$res = json_decode($resp,true);
-
if(time()>($modifytime+$res[‘expires_in’]-500)){
-
$resp = file_get_contents(“https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=Glg9WjlKApIOCzNpna9eZWxR&client_secret=Z68xhe8kGzzcgwLhcEwM8332GBK3inmd”);
-
file_put_contents($file,$resp);
-
$res = json_decode($resp,true);
-
}
-
$access_token = $res[‘access_token’];
-
$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’];
-
if($_POST[‘trystr’]==’trystr’){
-
$this->ajaxreturn(array(‘type’=>”trystr”,”msg”=>”试听”,’url’=>$url));
-
}
-
$name = $this->member_id.’_’.rand(1,10000).’_’.date(“YmdHis”).rand(1,10000).’.mp3′;
-
/* $audiodir = $_SERVER[‘DOCUMENT_ROOT’].’/mp3/’;
-
$audiofile = $audiodir.$name;
-
chmod($audiodir,0777);
-
$resps = file_get_contents($url);
-
chmod($audiofile,0777);
-
file_put_contents($audiofile,$resps,true);*/
-
//直接上传到七牛
-
// $url = “http://”.$_SERVER[‘HTTP_HOST’].’/mp3/’.$name;
-
$access_key = Config::AK;
-
$secret_key = Config::SK;
-
$fetch = urlsafe_base64_encode($url);
-
$to = urlsafe_base64_encode(Config::BUCKET_AUDIO_NAME.’:’.$name); //格式为 news2v:123.jpg
-
$url = ‘http://iovip.qbox.me/fetch/’. $fetch .’/to/’ . $to;
-
$access_token = generate_access_token($access_key, $secret_key, $url);
-
$header[] = ‘Content-Type: application/json’;
-
$header[] = ‘Authorization: QBox ‘. $access_token;
-
$curl = curl_init(‘iovip.qbox.me/fetch/’.$fetch.’/to/’.$to);
-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
-
curl_setopt($curl, CURLOPT_HEADER,1);
-
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
-
curl_setopt($curl, CURLOPT_POST, 1);
-
curl_setopt($curl, CURLOPT_TIMEOUT,60);
-
$con = curl_exec($curl);
-
$res = APP_AUDIO.’/’.$name;