Jdtts.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2020/3/9
  6. * Time: 13:51
  7. */
  8. namespace app\api\controller;
  9. use app\common\controller\Api;
  10. use GuzzleHttp\Client as Http;
  11. class Jdtts extends Api
  12. {
  13. public function test($text='')
  14. {
  15. $mData = $this->getSignkey();
  16. $text = "你好,人工智能!";
  17. $appkey='a42b852ceccba1b98462a43c78ab899a';
  18. $timestamp=$mData['timestamp'];
  19. $sign=$mData['sign'];
  20. /*测试地址:
  21.   https://stream-tts-dev.jd.com/tts/stream
  22. 正式地址:
  23.   https://stream-tts.jd.com/tts/stream*/
  24. // 这是接口文档: https://aidoc.jd.com/speech/tts-stream.htm
  25. $url = 'https://stream-tts-dev.jd.com/tts/stream?appkey='.$appkey.'&timestamp='.$timestamp.'&sign='.$sign.'&sp=1.0&sr=24000&text='.$text.'&vol=1.0&tim=0';
  26. $client = new Http([
  27. 'connect_timeout' => 10,
  28. 'timeout' => 30,
  29. 'http_errors' => true, //抛出异常 true是 false否
  30. 'verify' => false, //不验证ssl证书
  31. ]);
  32. $result = $client->get($url);
  33. /*$url = "https://stream-tts-dev.jd.com/tts/stream";
  34. $data = Http::get($url,
  35. [
  36. 'appkey'=>$appkey,
  37. 'timestamp'=>$timestamp,
  38. 'sign'=>$sign,
  39. 'sp'=>1.0,
  40. 'sr'=>24000,
  41. 'vol'=>1.0,
  42. 'tim'=>0,
  43. 'text'=>$text
  44. ]);*/
  45. /*var_dump();
  46. die();*/
  47. //$result->getBody()->getContents();
  48. echo json_encode(['code'=>$result->getStatusCode(), 'len'=>strlen($result->getBody()->getContents()), 'url'=>$url]);
  49. }
  50. public function getSignkey($flag=1){
  51. $appkey='a42b852ceccba1b98462a43c78ab899a';
  52. $SecretKey = '2badb3eb77b3e6eaec78915354df224f';
  53. $timestamp = $this->msectime();
  54. $sign = md5($SecretKey . $timestamp);
  55. if($flag==1){
  56. return [
  57. 'sign' => $sign,
  58. 'timestamp' => $timestamp
  59. ];
  60. }else{
  61. echo "appkey:{$appkey}===sign:{$sign}===timestamp:{$timestamp}";
  62. }
  63. }
  64. function msectime()
  65. {
  66. list($msec, $sec) = explode(' ', microtime());
  67. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  68. return $msectime;
  69. }
  70. }