Home  >  Article  >  Backend Development  >  Teach you step by step how to use PHP to connect to Baidu text recognition interface

Teach you step by step how to use PHP to connect to Baidu text recognition interface

王林
王林Original
2023-08-12 08:54:201381browse

Teach you step by step how to use PHP to connect to Baidu text recognition interface

Teach you step by step how to use PHP to connect to Baidu text recognition interface

Baidu text recognition is a powerful OCR (optical character recognition) technology that can Text content is converted into editable text. This article will describe in detail how to use PHP language to connect to Baidu text recognition interface, and provide corresponding code examples.

Step 1: Apply for Baidu Text Recognition API

First, you need to apply for a developer account on Baidu Smart Cloud and create a text recognition application. After creating the application, you will get an API Key and Secret Key, which will be used in the subsequent code.

Step 2: Install the necessary PHP extensions

In order to use the Baidu text recognition interface, you need to install two PHP extensions, curl and json. On your server, you can install it with the following command:

sudo apt-get install php-curl
sudo apt-get install php-json

Step 3: Write PHP code

<?php
// 设置API密钥
$apiKey = "your_api_key";
$secretKey = "your_secret_key";

// 要识别的图片路径
$imagePath = "path/to/image.jpg";

// 生成Access Token
$tokenURL = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={$apiKey}&client_secret={$secretKey}";
$response = file_get_contents($tokenURL);
$tokenArr = json_decode($response, true);
$accessToken = $tokenArr["access_token"];

// 构造请求参数
$requestURL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token={$accessToken}";
$imageData = file_get_contents($imagePath);
$imageBase64 = base64_encode($imageData);

$data = array(
    "image" => $imageBase64
);

// 发送请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

// 解析响应结果
$result = json_decode($response, true);

// 输出识别结果
if (isset($result["words_result"])) {
    foreach ($result["words_result"] as $item) {
        echo $item["words"] . "<br/>";
    }
}
?>

In the code, please replace your_api_key and your_secret_key Replace with your own API Key and Secret Key. At the same time, replace path/to/image.jpg with the path of the image you want to identify.

Step 4: Run the code

Save and run the above code, you will get the recognition results of the text in the picture.

Summary

Through the above simple steps, we successfully used PHP to connect to Baidu text recognition interface. You can modify and extend the code according to your own needs, adding more functions and visual interfaces. I hope this article will help you learn and apply Baidu text recognition!

The above is the detailed content of Teach you step by step how to use PHP to connect to Baidu text recognition interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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