PHP7中文手册2018 带注释 最新chm版
- php用百度语音技术生成离线mp3 可以用于在线阅读文章,在线读笑话的功能
- <?php
- $url = "http://tsn.baidu.com/text2audio";
- $cuid = "2457475";
- $apiKey = "申请Key";
- $secretKey = "申请Key";
- $auth_url = "http://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=".$apiKey."&client_secret=".$secretKey;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $auth_url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- $response = curl_exec($ch);
- if(curl_errno($ch))
- {
- print curl_error($ch);
- }
- curl_close($ch);
- $response = json_decode($response, true);
- $token = $response['access_token'];
- $array = array(
- 'tex'=>'懒人程序是一个神奇的网站,我不信。', //必填 合成的文本,使用UTF-8编码,请注意文本长度必须小于1024字节
- 'lan' => 'zh',
- 'tok'=>$token,// 必填 开放平台获取到的开发者 access_token
- 'ctp'=>1,// 必填 客户端类型选择,web端填写1
- 'cuid' =>$cuid,// 必填 用户唯一标识,用来区分用户,填写机器 MAC 地址或 IMEI 码,长度为60以内
- 'spd'=>5,// 选填 语速,取值0-9,默认为5中语速
- 'pit'=>9,// 选填 音调,取值0-9,默认为5中语调
- 'vol'=>9,// 选填 音量,取值0-9,默认为5中音量
- 'per'=>0,// 选填 发音人选择,取值0-1, 0为女声,1为男声,默认为女声
- );
- $header = array ('Content-Type:audio/mp3');
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- //curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array)); //data with URLEncode
- $response = curl_exec($ch);
- if(curl_errno($ch))
- {
- print curl_error($ch);
- }
- curl_close($ch);
- $file = file_put_contents("1.mp3",$response);
- if($file) {
- echo "生成成功";
- }
- ?>
转载请注明:谷谷点程序 » php用百度语音技术生成离线mp3 【原创】