IgrWsDemo.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace voice;
  3. class IgrWsDemo
  4. {
  5. protected $hostUrl; //http url 不支持解析 ws/wss schema
  6. protected $appid;
  7. protected $apiSecret;
  8. protected $apiKey;
  9. protected $file = "0.pcm";
  10. protected $statusFirstFrame = 0;
  11. protected $statusContinueFrame = 1;
  12. protected $statusLastFrame = 2;
  13. /**
  14. * 初始化参数
  15. * @return mixed
  16. */
  17. public function __construct() {
  18. $this->hostUrl = config("xunfei")["host"];
  19. $this->appid = config("xunfei")["appid"];
  20. $this->apiSecret = config("xunfei")["apiSecret"];
  21. $this->apiKey = config("xunfei")["apiKey"];
  22. }
  23. /**
  24. * 获取鉴权
  25. */
  26. public function getAuthorization() {
  27. // date 格式:UTC+0或GMT时区,RFC1123格式(Mon, 02 Jan 2006 15:04:05 GMT)。
  28. $date = gmstrftime("%a, %d %b %Y %T %Z",time());
  29. // 构建 authorization原始参数 注意保留换行符
  30. $authorization_origin = "host: ".$this->hostUrl."\ndate: ".$date."\nGET /v2/igr HTTP/1.1";
  31. // 获取签名后的摘要signature_sha。
  32. // 参考:https://www.it1352.com/1743824.html
  33. $signature_sha = hash_hmac('sha256', $authorization_origin, $this->apiSecret);
  34. // base 64 加密
  35. $signature64 = base64_encode($signature_sha);
  36. // 构建authorization请求参数
  37. $authorization = base64_encode('api_key='.$this->apiKey.', algorithm="hmac-sha256", headers="host date request-line", signature='.$signature64);
  38. $request_url = "wss://ws-api.xfyun.cn/v2/igr?authorization=".$authorization."&date=".$date."&host=".$this->hostUrl;
  39. return $request_url;
  40. }
  41. }