php 实现语音识别
使用 Google Cloud Speech-to-Text API
注册 Google Cloud 平台并启用 Speech-to-Text API,获取 API 密钥。安装 Google Cloud SDK 或直接通过 REST API 调用。

require 'vendor/autoload.php';
use Google\Cloud\Speech\V1\SpeechClient;
use Google\Cloud\Speech\V1\RecognitionAudio;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
$audioFile = 'path/to/audio.wav';
$content = file_get_contents($audioFile);
$audio = (new RecognitionAudio())->setContent($content);
$config = new RecognitionConfig();
$config->setEncoding(AudioEncoding::LINEAR16);
$config->setSampleRateHertz(16000);
$config->setLanguageCode('en-US');
$client = new SpeechClient();
$response = $client->recognize($config, $audio);
foreach ($response->getResults() as $result) {
echo $result->getAlternatives()[0]->getTranscript();
}
$client->close();
使用 Mozilla DeepSpeech 开源引擎
下载 DeepSpeech 预训练模型及依赖,通过 PHP 执行命令行调用或扩展集成。

$model = 'path/to/deepspeech-0.9.3-models.pbmm';
$scorer = 'path/to/deepspeech-0.9.3-models.scorer';
$audio = 'path/to/audio.wav';
$command = "deepspeech --model $model --scorer $scorer --audio $audio";
$output = shell_exec($command);
echo $output;
调用阿里云智能语音交互
阿里云提供实时语音识别服务,需开通服务并获取 AccessKey。
use AlibabaCloud\SDK\Dyplsapi\V20170525\Dyplsapi;
use Darabonba\OpenApi\Models\Config;
$config = new Config([
"accessKeyId" => "your_access_key",
"accessKeySecret" => "your_secret_key"
]);
$client = new Dyplsapi($config);
$params = [
"AudioFile" => base64_encode(file_get_contents('audio.wav')),
"Format" => "wav"
];
$response = $client->submitSpeechRecognize($params);
echo json_decode($response->body)->ResultText;
本地库 CMU Sphinx 集成
安装 PocketSphinx 及 PHP 扩展,适合离线场景。
$handle = sphinx_init([
'hmm' => '/usr/local/share/pocketsphinx/model/en-us',
'lm' => '/usr/local/share/pocketsphinx/model/en-us.lm.bin',
'dict' => '/usr/local/share/pocketsphinx/model/cmudict-en-us.dict'
]);
sphinx_process_raw($handle, file_get_contents('audio.raw'));
$result = sphinx_get_result($handle);
echo $result['text'];
sphinx_close($handle);
注意事项
- API 服务需处理网络延迟和计费问题。
- 本地引擎需考虑服务器性能和依赖兼容性。
- 音频格式需转换为 API 或引擎支持的格式(如 WAV、FLAC)。
- 实时识别需结合 WebSocket 或流式传输技术。






