首先通过HTTP请求调用百度或Google语音识别API,需处理音频格式、鉴权及JSON解析。以百度为例,先用API Key和Secret Key获取Access Token,再将PCM格式音频Base64编码后发送至其接口;Google则需配置服务账户密钥,使用SDK或REST调用,支持流式识别。注意音频格式、大小限制与网络超时,建议封装成类复用。

在PHP中调用语音识别API(如百度、Google Speech)主要通过HTTP请求将音频文件或实时音频数据发送到云端服务,再接收返回的文本结果。整个过程涉及音频格式处理、接口鉴权、网络请求和JSON解析。下面以百度语音识别和Google Speech-to-Text为例,介绍具体实现方式。
百度语音识别API调用方法
百度AI开放平台提供中文语音识别服务,支持多种采样率和音频格式。调用前需注册账号并创建语音识别应用,获取API Key和Secret Key。
步骤如下:- 使用API Key和Secret Key获取Access Token
- 准备符合要求的音频文件(WAV/PCM,16bit位深,单声道)
- 将音频数据Base64编码后发送POST请求至识别接口
获取Access Token示例代码:
function getBaiduToken($apiKey, $secretKey) {
$url = "https://aip.baidubce.com/oauth/2.0/token";
$post_data = [
'grant_type' => 'client_credentials',
'client_id' => $apiKey,
'client_secret' => $secretKey
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
return $result['access_token'];
}
调用语音识别接口:
立即学习“PHP免费学习笔记(深入)”;
function baiduSpeechRecognize($audioPath, $token) {
$audioData = file_get_contents($audioPath);
$base64Audio = base64_encode($audioData);
$length = filesize($audioPath);
$data = [
"format" => "pcm",
"rate" => 16000,
"channel" => 1,
"cuid" => "your_device_id",
"token" => $token,
"len" => $length,
"speech" => $base64Audio
];
$jsonData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://vop.baidu.com/pro_api");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
Google Speech-to-Text API调用方法
Google Cloud Speech-to-Text支持多语言识别,功能强大。使用前需开通Google Cloud项目,启用Speech API,并配置服务账户密钥文件。
实现要点:- 下载服务账户JSON密钥文件
- 安装Google Cloud PHP SDK(推荐使用Composer)
- 设置环境变量GOOGLE_APPLICATION_CREDENTIALS指向密钥文件
使用SDK进行识别:
require 'vendor/autoload.php';
use Google\Cloud\Speech\V1\SpeechClient;
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\RecognitionAudio;
$client = new SpeechClient();
$config = new RecognitionConfig([
'encoding' => AudioEncoding::LINEAR16,
'sample_rate_hertz' => 16000,
'language_code' => 'zh-CN'
]);
$audio = new RecognitionAudio();
$audio->setContent(file_get_contents('audio.wav'));
$response = $client->recognize($config, $audio);
$results = $response->getResults();
foreach ($results as $result) {
$alternatives = $result->getAlternatives();
$mostLikely = $alternatives[0];
echo $mostLikely->getTranscript();
}
$client->close();
若不使用SDK,也可直接调用REST接口,需手动处理OAuth 2.0令牌和JSON请求体。
注意事项与常见问题
实际开发中需注意以下几点:
- 音频格式必须符合API要求,否则返回错误或识别失败
- 百度接口对单个音频大小有限制(通常不超过10MB)
- Google API支持流式识别,适合长音频或实时场景
- 网络请求需处理超时和重试机制
- 生产环境应缓存Access Token避免频繁获取
对于非标准格式音频,可用FFmpeg转换:
ffmpeg -i input.mp3 -ar 16000 -ac 1 -f s16le output.pcm 基本上就这些。只要准备好认证信息、规范音频格式、正确构造请求,PHP调用语音识别并不复杂,但细节容易出错,建议封装成类便于复用。











