iFLYTEK-OP / websdk-php

讯飞开放平台AI能力-PHPSDK
Apache License 2.0
21 stars 5 forks source link

guzzlehttp/psr7 #19

Closed dy7338 closed 1 year ago

dy7338 commented 2 years ago

this package for guzzlehttp/psr7 version is too low

Lazybin commented 1 year ago

same question

dy7338 commented 1 year ago

demo

/**
     * 科大讯飞的语音合成
     *
     * @param $content
     *
     * @return array
     */
    public function keDaTts ($content = '') {
        $appId = env ('KEDA_APPID', '');

        $URI          = 'wss://tts-api.xfyun.cn/v2/tts';
        $HOST         = 'tts-api.xfyun.cn';
        $REQUEST_LINE = 'GET /v2/tts HTTP/1.1';

        $config = [
            'ttp' => 'cssml',
            'aue' => 'lame',
            'sfl' => 1,
            'auf' => null,
            'vcn' => 'xiaoyu', //x3_mingze
            'tte' => 'UTF8',
            'reg' => '2',
            'rdn' => '0',
            'ent' => ''
        ];

        $ws_url = $this->keDasignUriV1 ($URI, [
            'appId'       => $appId,
            'apiKey'      => env ('KEDA_API_KEY', ''),
            'apiSecret'   => env ('KEDA_API_SECRET', ''),
            'host'        => $HOST,
            'requestLine' => $REQUEST_LINE,
        ]);

        // 音频处理

        $data = json_encode (
            [
                'common'   => [
                    'app_id' => $appId
                ],
                'business' => $config,
                'data'     => [
                    'text'   => base64_encode ($this->breakContent ($content)),
                    'status' => 2
                ]
            ],
            JSON_UNESCAPED_UNICODE
        );

        $client = new Client($ws_url);
        $client->setTimeout (300);
        // 开始发送数据
        $result = '';
        try {
            $client->send ($data);
            $printSid = true;
            while (true) {
                $message = json_decode ($client->receive (), true);

                if ($message['code'] !== 0) {
                    Log::error ('科大音频合成错误:' . json_encode ($message, JSON_UNESCAPED_UNICODE));
                }
                if ($printSid) {
                    Log::error ('科大音频合成错误->sid-' . $message['sid']);
                    $printSid = false;
                }
                switch ($message['data']['status']) {
                    case 1:
                        $result .= base64_decode ($message['data']['audio']);
                        break;
                    case 2:
                        $result .= base64_decode ($message['data']['audio']);
                        break 2;
                }
            }

        } catch (\Exception $e) {
            Log::error ('科大合成语音错误 ->' . $e->getCode () . '->' . $e->getFile () . ' -> ' . $e->getLine () . ' -> ' . $e->getMessage ());

            return $this->format_error ($e->getMessage ());
        }

        return $this->saveAudioToStorage (base64_encode ($result));

    }

    /**
     * 根据secret对uri进行签名,返回签名后的uri
     *
     * @param  string  $uri     待签名的uri
     * @param  array   $secret  秘钥信息
     *
     * @return  string
     */
    private function keDasignUriV1 ($uri, $secret) {
        $apiKey       = $secret['apiKey'];
        $apiSecret    = $secret['apiSecret'];
        $host         = $secret['host'];
        $request_line = $secret['requestLine'];
        $date         = empty($secret['date']) ? gmstrftime ("%a, %d %b %Y %T %Z", time ()) : $secret['date'];

        $signature_origin = "host: $host\ndate: $date\n$request_line";
        $signature_sha    = hash_hmac ('sha256', $signature_origin, $apiSecret, true);
        $signature        = base64_encode ($signature_sha);

        $authrization = base64_encode ("api_key=\"$apiKey\",algorithm=\"hmac-sha256\",headers=\"host date request-line\",signature=\"$signature\"");
        $uri          = $uri . '?' . http_build_query ([
                                                           'host'          => $host,
                                                           'date'          => $date,
                                                           'authorization' => $authrization
                                                       ]);

        return $uri;
    }