Home  >  Article  >  Backend Development  >  How does PHP connect to Baidu image review interface?

How does PHP connect to Baidu image review interface?

WBOY
WBOYOriginal
2023-08-27 13:12:32890browse

How does PHP connect to Baidu image review interface?

How does PHP connect to Baidu image review interface?

  1. Introduction to Baidu image review interface

Baidu image review interface is an interface that can determine whether there are violations by analyzing the image content. By reviewing images in various aspects such as identification, pornography, sexiness, politics, vulgarity, and violence, it helps developers filter out image content that does not meet requirements and improves the content security of applications.

  1. Preparation work

Before starting to connect to Baidu image review interface, you need to prepare the following work:

  • Baidu AI Open Platform Account, obtain the API Key and Secret Key of the application.
  • PHP development environment, ensure that the PHP version is above 5.3.
  1. Create a PHP script file for reviewing images

Create a new PHP file named "image_review.php" and write the following code in the file:

<?php

// 获取AccessToken
$clientId = 'your_client_id';
$clientSecret = 'your_client_secret';

$tokenUrl = 'https://aip.baidubce.com/oauth/2.0/token';
$params = array(
    'grant_type' => 'client_credentials',
    'client_id' => $clientId,
    'client_secret' => $clientSecret
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $tokenUrl.'?'.http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$json = json_decode($response);
$accessToken = $json->access_token;

// 调用图像审核接口
$imageUrl = 'http://example.com/image.png';
$reviewUrl = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token='.$accessToken;

$params = array(
    'imgUrl' => $imageUrl
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $reviewUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$json = json_decode($response);

// 处理审核结果
if ($json->conclusionType == 1) {
    echo '图片正常';
} else {
    echo '图片违规,不合规类型:';
    foreach ($json->data as $item) {
        echo $item->msg.' ';
    }
}

curl_close($ch);

In the above code, you need to replace "your_client_id" and "your_client_secret" with the API Key and Secret Key of your Baidu AI open platform. "http://example.com/image.png" is the image URL to be reviewed, you can replace it with your own image URL.

  1. Run the PHP script file for auditing images

Upload the written PHP script file to your PHP development environment and access the script file through the browser, that is Code that can run the review image.

The image review results in the code example will be displayed on the page. If the image is normal, "Image is normal" will be output; if the image violates the rules, "Image violation, non-compliance type:" will be output. and the specific type of violation.

Summary

Through the above steps, you can easily connect to the Baidu image review interface to determine and filter image content violations. At the same time, you can further optimize and expand the code according to your needs to achieve more personalized and flexible applications.

Reference materials:

  • Baidu AI Open Platform-Image review: https://ai.baidu.com/tech/imagecensoring/
  • Baidu AI Open Platform -Get AccessToken: https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu

The above is the detailed content of How does PHP connect to Baidu image review 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