Current Location: Home> Latest Articles> How to Integrate Baidu Image Review API with PHP for Content Moderation

How to Integrate Baidu Image Review API with PHP for Content Moderation

gitbox 2025-06-13

1. Overview

With the rapid development of the internet, the application of image moderation technology has become increasingly widespread. Baidu's image review service is free and reliable, making it the preferred choice for many website developers and operators. This article will explain in detail how to integrate Baidu's image review API with PHP to enable automated image content monitoring.

2. Prerequisites

2.1 Obtain Baidu AI Open Platform Account

First, visit the Baidu AI Open Platform website and register for an account at https://ai.baidu.com/.

2.2 Create an Application

After logging into the Baidu AI Open Platform, go to the console and select "Image Technology" and then "Image Review". Click the "Create Application" button, select the review service, and click "Create Now" to complete the application creation.

Once the application is created, you will receive an API Key and Secret Key, which are required in the following code.

3. PHP Code Implementation

3.1 Install the PHP SDK

Baidu provides a PHP SDK that can be installed via Composer or by downloading the source code directly. It is recommended to use Composer for installation by running the following command:

composer require baidu-aip/sdk

3.2 Implementation Code

Insert your API Key and Secret Key into the code below, and modify the image path accordingly:


require_once './vendor/autoload.php';
use BaiDu\Aip\ImageCensor;
<p>// Configuration information<br>
$appId = 'your app id';<br>
$apiKey = 'your api key';<br>
$secretKey = 'your secret key';</p>
<p>// Create the image review object<br>
$censor = new ImageCensor($appId, $apiKey, $secretKey);</p>
<p>// Image to be reviewed<br>
$imagePath = 'path/to/your/image';</p>
<p>// Call the review API<br>
$result = $censor->imageCensorUserDefined(file_get_contents($imagePath));</p>
<p>// Print result<br>
var_dump($result);<br>

4. Notes

4.1 Request Rate Limiting

To ensure service quality, Baidu AI Open Platform imposes rate limits on the number of requests each application can make. If requests are sent too frequently, the account may be disabled, so developers should control the request frequency.

4.2 Explanation of Review Results

The image review result is returned in JSON format, and developers need to parse each field. For example, if the "conclusion" field value is "non-compliant", it means the image did not pass the review.

5. Conclusion

This article explained how to integrate Baidu Image Review API using PHP. First, you need to register for a Baidu AI Open Platform account, create an application, and obtain the API Key and Secret Key. Then, the PHP SDK can be used for integration, and the review results can be parsed to determine if the image complies with the rules. Developers should also pay attention to the request rate limit when using the API.