Home > Backend Development > PHP Tutorial > How to implement access authentication of Baidu Wenxin Yiyan API in PHP development?

How to implement access authentication of Baidu Wenxin Yiyan API in PHP development?

王林
Release: 2023-08-27 15:34:02
Original
1167 people have browsed it

How to implement access authentication of Baidu Wenxin Yiyan API in PHP development?

How to implement access authentication of Baidu Wenxin Yiyan API in PHP development?

During the PHP development process, if you need to use Baidu Wenxin Yiyan API, access authentication is required to ensure that only authorized users can access the API normally. This article will introduce how to implement access authentication of Baidu Wenxin Yiyan API in PHP and provide relevant code examples.

First of all, before using Baidu Wenxinyiyan API, you need to register a Baidu developer account and create an application. Through Baidu Open Platform, you can obtain an API Key and Secret Key. These two parameters are important parameters for access authentication.

To implement access authentication of Baidu Wenxin Yiyan API in PHP, you can use the interface class BaiduOAuth2 provided by Baidu. The following is a basic access authentication example:

<?php
require_once('BaiduOAuth2.php');  //引入百度OAuth2类

$apiKey = 'Your_API_Key';  //替换为您在百度开放平台获取的API Key
$secretKey = 'Your_Secret_Key';  //替换为您在百度开放平台获取的Secret Key

$oauth = new BaiduOAuth2($apiKey, $secretKey);  //实例化百度OAuth2类

//获取访问令牌
$res = $oauth->getAccessToken();
if (!$res) {
    echo '获取访问令牌失败!';
    exit;
}

$accessToken = $oauth->getToken();  //获取访问令牌
$expiresIn = $oauth->getExpiresIn();  //获取访问令牌的有效期

//开始访问百度文心一言API
$url = 'https://aip.baidubce.com/rpc/2.0/unit/bot/chat';

//设置请求参数
$data = array(
    'access_token' => $accessToken,
    'query' => '你好'
);

$options = array(
    'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query($data)
    )
);

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

//输出API调用结果
echo $result;
?>
Copy after login

In the above code, the Baidu OAuth2 class file BaiduOAuth2.php is first introduced, and the BaiduOAuth2 class object is instantiated through apiKey and secretKey. Then use the getAccessToken() method to obtain the access token, and obtain the token and validity period through the getToken() and getExpiresIn() methods. Next, you can set the request parameters according to your needs and send a POST request through the file_get_contents() function to call Baidu Wenxin Yiyan API.

It should be noted that this access authentication example only provides basic logic, and you need to modify and extend it according to the actual situation. For example, you may need to add logic such as exception handling and error prompts to enhance the robustness and user experience of the program.

Summary: Through the above method, you can achieve access authentication of Baidu Wenxin Yiyan API in PHP development. Only authorized users can successfully access the API and obtain the required data. At the same time, we also provide a basic code example, hoping to be helpful to your development work. If you have any questions, please feel free to consult us.

The above is the detailed content of How to implement access authentication of Baidu Wenxin Yiyan API in PHP development?. 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