How does PHP connect to Baidu’s semantic emotion recognition interface?
Overview: Baidu Semantic Emotion Recognition Interface is an emotion analysis tool based on artificial intelligence technology that can analyze the emotional tendencies in text, allowing developers to better understand the user's emotional state. This article will introduce how to use PHP to connect to Baidu's semantic emotion recognition interface to implement emotion analysis functions.
Step 1: Apply for Baidu Semantic Emotion Recognition Interface
First, we need to register a Baidu Smart Cloud account and create an application. Then, enter the application management interface, find the "Natural Language Processing" module and enable the "Sentiment Analysis" function. In this way, you can get the API Key and Secret Key of Baidu's semantic emotion recognition interface.
Step 2: Install PHP HTTP request library
In order to send HTTP requests, we need to install PHP's HTTP request library. Here we use the Guzzle library to complete it, which can be installed through Composer. Create a composer.json file in the project root directory and add the following content:
{ "require": { "guzzlehttp/guzzle": "^7.0" } }
Then run the composer install
command to install the Guzzle library.
Step 3: Write PHP code
Create a file named sentiment_analysis.php and add the following code:
<?php require 'vendor/autoload.php'; use GuzzleHttpClient; // 设置API Key和Secret Key $api_key = 'YOUR_API_KEY'; $secret_key = 'YOUR_SECRET_KEY'; // 设置请求URL和参数 $url = 'https://aip.baidubce.com/oauth/2.0/token'; $params = array( 'grant_type' => 'client_credentials', 'client_id' => $api_key, 'client_secret' => $secret_key ); // 发送HTTP请求获取Access Token $client = new Client(); $response = $client->request('POST', $url, [ 'form_params' => $params ]); $result = json_decode($response->getBody(), true); $access_token = $result['access_token']; // 设置情感分析请求URL和参数 $url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify'; $text = '今天天气非常好!'; // 发送情感分析请求 $response = $client->request('POST', $url, [ 'headers' => [ 'Content-Type' => 'application/json' ], 'query' => [ 'access_token' => $access_token ], 'body' => '{"text": "'.$text.'"}' ]); $result = json_decode($response->getBody(), true); // 输出情感倾向 echo '情感倾向:'.$result['items'][0]['sentiment'].'<br>';
Please replace YOUR_API_KEY and YOUR_SECRET_KEY with the ones you obtained in step one API Key and Secret Key.
Step 4: Run the code and get the results
Open the command line tool, switch to the directory where sentiment_analysis.php is located, and run the following command:
php sentiment_analysis.php
You will see the output sentiment tendency result.
Summary: This article introduces how to connect to Baidu's semantic emotion recognition interface through PHP code examples. Through this example, we can quickly implement the text sentiment analysis function and make corresponding decisions based on the user's emotional tendencies. Let’s try it together!
The above is the detailed content of How does PHP connect to Baidu's semantic emotion recognition interface?. For more information, please follow other related articles on the PHP Chinese website!