123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Elton
- * Date: 2020/3/9
- * Time: 13:51
- */
- namespace app\api\controller;
- use app\common\controller\Api;
- use GuzzleHttp\Client as Http;
- class Jdtts extends Api
- {
- public function test($text='')
- {
- $mData = $this->getSignkey();
- $text = "你好,人工智能!";
- $appkey='a42b852ceccba1b98462a43c78ab899a';
- $timestamp=$mData['timestamp'];
- $sign=$mData['sign'];
- /*测试地址:
- https://stream-tts-dev.jd.com/tts/stream
- 正式地址:
- https://stream-tts.jd.com/tts/stream*/
- // 这是接口文档: https://aidoc.jd.com/speech/tts-stream.htm
- $url = 'https://stream-tts-dev.jd.com/tts/stream?appkey='.$appkey.'×tamp='.$timestamp.'&sign='.$sign.'&sp=1.0&sr=24000&text='.$text.'&vol=1.0&tim=0';
- $client = new Http([
- 'connect_timeout' => 10,
- 'timeout' => 30,
- 'http_errors' => true, //抛出异常 true是 false否
- 'verify' => false, //不验证ssl证书
- ]);
- $result = $client->get($url);
- /*$url = "https://stream-tts-dev.jd.com/tts/stream";
- $data = Http::get($url,
- [
- 'appkey'=>$appkey,
- 'timestamp'=>$timestamp,
- 'sign'=>$sign,
- 'sp'=>1.0,
- 'sr'=>24000,
- 'vol'=>1.0,
- 'tim'=>0,
- 'text'=>$text
- ]);*/
- /*var_dump();
- die();*/
- //$result->getBody()->getContents();
- echo json_encode(['code'=>$result->getStatusCode(), 'len'=>strlen($result->getBody()->getContents()), 'url'=>$url]);
- }
- public function getSignkey($flag=1){
- $appkey='a42b852ceccba1b98462a43c78ab899a';
- $SecretKey = '2badb3eb77b3e6eaec78915354df224f';
- $timestamp = $this->msectime();
- $sign = md5($SecretKey . $timestamp);
- if($flag==1){
- return [
- 'sign' => $sign,
- 'timestamp' => $timestamp
- ];
- }else{
- echo "appkey:{$appkey}===sign:{$sign}===timestamp:{$timestamp}";
- }
- }
- function msectime()
- {
- list($msec, $sec) = explode(' ', microtime());
- $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- return $msectime;
- }
- }
|