读英文单词,百度云语音合成
AI 提取并生成html文件





修改js
<script>
layer.alert('李安妮开始练单词?',{icon:6,btn:['对的']});
function pronounce(word) {
$.ajax({
url:'https://www.xxx.com/read/api.php',
type: 'POST',
data: {
word:word,
},
beforeSend: function (){
},
success : function(res){
const audio = new Audio(res);
audio.play();
}
});
}
</script>php接口,调用百度支语音合成接口
百度云语言合成文档:https://console.bce.baidu.com/ai-engine/speech/overview/index
<?php
class Sample {
const API_KEY = "你的API_KEY ";
const SECRET_KEY = "你的API_KEYSECRET_KEY ";
public function run($word) {
//判断音频文件是否存在,存在则直接返回
if(file_exists('./mp3/'.$word.'.mp3')){
return 'https://www.blockphp.com/read/mp3/'.$word.'.mp3?c=1';
}
$token = $this->getAccessToken();
//exit($token);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://tsn.baidu.com/text2audio",
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => http_build_query(array(
'tex' => $word,
'tok' => $token,
'cuid' => 'L32v25cV532rSIhCfyrmbhk38bWp3ZQC',
'ctp' => '1',
'lan' => 'zh',
'spd' => '5',
'pit' => '5',
'vol' => '5',
'per' => '0',
'aue' => '3'
)),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Accept: */*'
),
));
$response = curl_exec($curl);
curl_close($curl);
file_put_contents('./mp3/'.$word.'.mp3',$response);
return 'https://www.blockphp.com/read/mp3/'.$word.'.mp3';
//return $response;
}
/**
* 使用 AK,SK 生成鉴权签名(Access Token)
* @return string 鉴权签名信息(Access Token)
*/
private function getAccessToken(){
$curl = curl_init();
$postData = array(
'grant_type' => 'client_credentials',
'client_id' => self::API_KEY,
'client_secret' => self::SECRET_KEY
);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://aip.baidubce.com/oauth/2.0/token',
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => http_build_query($postData)
));
$response = curl_exec($curl);
curl_close($curl);
$rtn = json_decode($response);
return $rtn->access_token;
}
}
$rtn = (new Sample())->run($_POST['word']);
exit($rtn);