Home > Article > Backend Development > How does PHP connect to Baidu image review interface?
How does PHP connect 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.
Before starting to connect to Baidu image review interface, you need to prepare the following work:
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.
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:
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!