Use PHP to develop and implement the permission management and authentication method of Baidu Wenxin Yiyan API interface

WBOY
Release: 2023-08-26 20:18:02
Original
915 people have browsed it

Use PHP to develop and implement the permission management and authentication method of Baidu Wenxin Yiyan API interface

Using PHP to develop and implement the permission management and authentication method of Baidu Wenxin Yiyan API interface

[Introduction]
Baidu Wenxin Yiyan is a platform that provides random API interface to obtain beautiful sentences, which can be used to beautify websites or applications and express emotions. When using this interface, permission management and authentication are required to ensure the security and correctness of the interface. This article will introduce how to use PHP to develop sample code that implements the permission management and authentication methods of Baidu Wenxinyiyan API interface.

[Preparation]
Before we start, we need to prepare some necessary information and environment:

  1. Register a Baidu AI open platform account, create an application, and obtain API Key and Secret Key.
  2. Install the PHP environment and ensure that the curl extension is installed for HTTP requests.

[Code Example]
The following is a sample code that uses PHP to implement permission management and authentication of Baidu Wenxin Yiyan API interface:

<?php

// 百度文心一言API接口地址
$url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify";

// API Key和Secret Key
$apiKey = "your_api_key";
$secretKey = "your_secret_key";

// 请求参数
$params = array(
    "text" => "这是一句话"
);

// 计算签名
$timestamp = time();
$nonce = uniqid();
$signature = md5($apiKey . $timestamp . $nonce . $secretKey);

// 设置请求头
$headers = array(
    "Content-Type: application/json",
    "charset: utf-8",
    "apikey: " . $apiKey,
    "signature: " . $signature,
    "timestamp: " . $timestamp,
    "nonce: " . $nonce
);

// 发起HTTP请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
$result = curl_exec($ch);
curl_close($ch);

// 处理响应结果
$data = json_decode($result, true);
if(isset($data['error_code'])) {
    echo "请求失败:" . $data['error_msg'];
} else {
    echo "情感极性:" . $data['items'][0]['sentiment'];
}

?>
Copy after login

[Code Analysis]
The above example code implements permission management and authentication by calculating signatures and setting request headers. The specific steps are as follows:

  1. Define the interface address, API Key and Secret Key.
  2. Set request parameters, only one sentence is included here as an example.
  3. Calculate the signature, concatenate the API Key, Secret Key, timestamp and random string, and then perform MD5 hashing.
  4. Set request headers, including ContentType, charset, apikey, signature, timestamp and nonce.
  5. Initiate an HTTP request, use the curl library to send a POST request, and convert the parameters into JSON format.
  6. Process the response result, parse the returned JSON string, and output the emotional polarity.

[Notes]
In actual use, you need to replace "your_api_key" and "your_secret_key" in the sample code with your own API Key and Secret Key. In addition, the parameters and code for processing response results need to be adjusted according to actual needs.

[Summary]
Through the code examples in this article, we can learn how to use PHP to develop and implement the permission management and authentication methods of Baidu Wenxin Yiyan API interface. This ensures the security and correctness of the interface and provides users of the website or application with beautiful sentences to enjoy and use. You can also learn how to use PHP to make HTTP requests and process JSON data. I hope readers can carry out actual development and application based on the sample code in this article.

The above is the detailed content of Use PHP to develop and implement the permission management and authentication method of Baidu Wenxin Yiyan API interface. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template