登录
首页 >  文章 >  php教程

PHP调用百度语音API实现转写

时间:2026-03-20 21:26:45 188浏览 收藏

本文详细讲解了如何使用PHP调用百度语音识别(ASR)API实现高效、稳定的语音转文字功能,涵盖从百度AI平台注册应用获取API Key与Secret Key、通过cURL请求动态获取并合理缓存Access Token,到将本地音频文件(如WAV格式)Base64编码后发送至百度ASR接口完成识别的完整技术链路,并附有可直接运行的实战代码与关键注意事项(如音频格式/采样率要求、10MB大小限制、错误处理和生产环境优化建议),为开发者提供了一站式、开箱即用的语音识别集成方案。

php调用语音识别接口_php调用百度语音API实现转换

要使用PHP调用百度语音识别API实现语音转文字,关键在于获取Access Token、上传音频文件并发送请求到百度ASR接口。整个过程需要遵循百度AI开放平台的规范,下面一步步说明如何实现。

1. 获取百度AI平台权限

在调用百度语音识别API前,必须先注册百度AI开放平台账号,并创建应用以获取凭证信息。

进入 百度AI开放平台(https://ai.baidu.com),选择“语音识别”服务,创建应用后会得到:

  • API Key
  • Secret Key

通过这两个密钥可以获取Access Token,这是调用API的必要参数。

2. 获取Access Token

Access Token是调用百度API的身份令牌,有效期一般为30天,可通过以下接口获取:

https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【API Key】&client_secret=【Secret Key】

使用PHP的cURL发送请求获取Token:

function getAccessToken($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, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($response, true);
    return $result['access_token'];
}

3. 调用语音识别API

百度语音识别接口支持多种格式(如pcm、wav、amr等),采样率需为8000或16000Hz。以下是上传音频并识别的示例代码:

function speechToText($audioFilePath, $format = 'wav', $rate = 16000, $token) {
    $speech = file_get_contents($audioFilePath);
    $len = filesize($audioFilePath);
    $speech = base64_encode($speech);

    $data = [
        "format" => $format,
        "rate" => $rate,
        "channel" => 1,
        "cuid" => "your_unique_id", // 可以是设备ID或随机字符串
        "token" => $token,
        "speech" => $speech,
        "len" => $len
    ];

    $json_data = json_encode($data);

    $url = "https://vop.baidubce.com/v1/recognition/simple";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Content-Length: ' . strlen($json_data)
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}

调用方式:

$apiKey = '你的API Key';
$secretKey = '你的Secret Key';
$token = getAccessToken($apiKey, $secretKey);

$result = speechToText('test.wav', 'wav', 16000, $token);
if (isset($result['result'])) {
    echo "识别结果:" . $result['result'][0];
} else {
    echo "识别失败:" . $result['err_msg'];
}

4. 注意事项

实际使用中需要注意以下几点:

  • 音频文件大小不能超过10MB
  • 推荐使用WAV格式,PCM编码,单声道
  • Access Token应缓存,避免频繁请求
  • 生产环境建议添加错误重试和日志记录

基本上就这些。只要拿到Token,正确封装音频数据,就能顺利实现语音转文字功能。

今天关于《PHP调用百度语音API实现转写》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

资料下载
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>