79 lines
2.5 KiB
PHP
79 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace app\api\logic;
|
|
|
|
use React\EventLoop\Factory;
|
|
use React\Socket\Connector;
|
|
use React\Http\Browser;
|
|
use React\Promise\Timer\Timeout;
|
|
use React\Stream\ThroughStream;
|
|
use React\WebSocket\ConnectionContext;
|
|
use React\WebSocket\WebSocketFactory;
|
|
class RealTimeASRClient
|
|
{
|
|
/**
|
|
* 根据所提供信息返回签名
|
|
*
|
|
* @param string $appId appid
|
|
* @param string $secretKey secretKey
|
|
* @param string $timestamp 时间戳,不传的话使用系统时间
|
|
* @return string
|
|
*/
|
|
function sign_V1($appId, $secretKey, $timestamp = null){
|
|
$timestamp = $timestamp ?: time();
|
|
$baseString = $appId . $timestamp;
|
|
$signa_origin = hash_hmac('sha1', md5($baseString), $secretKey, true);
|
|
return base64_encode($signa_origin);
|
|
}
|
|
|
|
|
|
function req(){
|
|
$loop = Factory::create();
|
|
|
|
$connector = new Connector($loop);
|
|
$browser = new Browser($connector);
|
|
|
|
$appid = 'your_appid';
|
|
$ts = strval(time());
|
|
$apiKey = 'your_apiKey';
|
|
|
|
// 生成signa
|
|
$baseString = $appid . $ts;
|
|
$md5BaseString = md5($baseString);
|
|
$signa = base64_encode(hash_hmac('sha1', $md5BaseString, $apiKey, true));
|
|
|
|
$url = "ws://rtasr.xfyun.cn/v1/ws?appid={$appid}&ts={$ts}&signa={$signa}";
|
|
|
|
$browser->request('GET', $url)
|
|
->then(function ($request) use ($loop) {
|
|
$request->on('response', function ($response) use ($loop) {
|
|
$response->on('data', function ($chunk) {
|
|
echo "Received: " . $chunk . PHP_EOL;
|
|
});
|
|
|
|
$response->on('close', function () {
|
|
echo "Connection closed." . PHP_EOL;
|
|
});
|
|
|
|
// 创建一个通过流,用于发送音频数据
|
|
$stream = new ThroughStream();
|
|
$stream->pipe($response);
|
|
|
|
// 这里可以发送音频数据,假设你有一个音频文件
|
|
// 注意:这只是一个示例,你需要根据实际情况发送数据
|
|
// $audioData = file_get_contents('/path/to/audio.pcm');
|
|
// $stream->write($audioData);
|
|
|
|
// 发送结束标志
|
|
$stream->end(json_encode(["end" => true]));
|
|
});
|
|
})
|
|
->then(null, function ($e) {
|
|
echo "Error: " . $e->getMessage() . PHP_EOL;
|
|
});
|
|
|
|
$loop->run();
|
|
}
|
|
}
|
|
|