Home > Backend Development > PHP Tutorial > How to connect PHP to Baidu speech recognition interface?

How to connect PHP to Baidu speech recognition interface?

WBOY
Release: 2023-08-13 22:30:01
Original
742 people have browsed it

How to connect PHP to Baidu speech recognition interface?

How does PHP connect to Baidu speech recognition interface?

With the rapid development of artificial intelligence, speech recognition technology has gradually become an important part of our daily lives. Baidu speech recognition interface is a powerful tool that can help us convert sounds into text, adding more interactive methods to our applications. This article will introduce how to use PHP to connect to Baidu speech recognition interface, and attach corresponding code examples.

First, we need to register a Baidu developer account and create an application in order to obtain the API Key and Secret Key. These keys will be used to connect to the Baidu speech recognition interface.

Next, we need to use PHP to send an HTTP request to call the Baidu speech recognition interface. We can use the cURL library to send a POST request and use the recording file as the requested data. The following is a sample code:

<?php
// 设置请求的URL
$url = 'https://vop.baidu.com/server_api';
// 设置参数
$params = array(
    'format' => 'pcm',
    'dev_pid' => 1536,
    'token' => 'YOUR_ACCESS_TOKEN',
    'cuid' => 'YOUR_CUID',
    'len' => filesize('path/to/your/audio/file.pcm'),
    'speech' => base64_encode(file_get_contents('path/to/your/audio/file.pcm')),
);
// 设置请求头部
$headers = array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen(json_encode($params)),
);
// 初始化cURL
$ch = curl_init();
// 设置cURL选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 发送HTTP请求
$response = curl_exec($ch);
// 关闭cURL
curl_close($ch);
// 处理响应结果
$result = json_decode($response, true);
if ($result && isset($result['result'])) {
    // 打印识别结果
    echo $result['result'][0];
} else {
    // 打印错误信息
    echo $result['err_msg'];
}
?>
Copy after login

In the above code, we first set the requested URL and set the required parameters as an associative array. Then, we set the request headers, including Content-Type and Content-Length. Next, we initialize a cURL handle using the cURL library and set the appropriate options. Finally, we send an HTTP request and get the response back.

It should be noted that code>YOUR_ACCESS_TOKEN and YOUR_CUID need to be replaced with your own access token and client unique identifier of the Baidu speech recognition interface. In addition, path/to/your/audio/file.pcm needs to be replaced with your own voice file path.

The above are the basic steps and code examples for using PHP to connect to Baidu speech recognition interface. Hope this helps!

The above is the detailed content of How to connect PHP to Baidu speech recognition interface?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template