PHP connects to Baidu Wenxin Yiyan API to obtain the number control method of specific types of sentences

WBOY
Release: 2023-08-27 16:02:01
Original
1176 people have browsed it

PHP connects to Baidu Wenxin Yiyan API to obtain the number control method of specific types of sentences

PHP connects to Baidu Wenxin Yiyan API to obtain the number control method of a specific type of sentence

Introduction
Baidu Wenxin Yiyan is an API that provides a sentence service, which can provide various types of sentences, such as inspirational, philosophical, love, etc. This article will introduce how to use PHP to connect to Baidu Wenxin Yiyan API and control the number of sentences according to needs.

Steps

  1. Apply for Baidu Wenxin Yiyan API permission.
    First, you need to apply for an account on Baidu Open Cloud Platform and create an application. After successful application, you will receive an App Key and App Secret.
  2. Generate signature
    Before sending a request to Baidu Wenxin Yiyan API, you need to generate a signature to ensure data security. The signature is generated by encrypting the request parameters according to certain rules.

    function generateSignature($params, $appSecret) {
        ksort($params);
        $signStr = '';
        foreach ($params as $key => $value) {
             $signStr .= $key . $value;
        }
        return md5($signStr . $appSecret);
    }
    Copy after login
  3. Building request parameters
    When building request parameters, you need to pass in the following parameters:

    • appKey: App Key of the application
    • sign: Signature
    • type: The type of sentence, such as inspirational, philosophical, love, etc.
    • count: The number of returned sentences
    • charset: Character encoding, generally using UTF -8 can
    $params = array(
        'appKey' => $appKey,
        'type' => 'philosophy',
        'count' => 10,
        'charset' => 'UTF-8',
    );
    $params['sign'] = generateSignature($params, $appSecret);
    Copy after login
  4. Send a request
    Using PHP's cURL library can easily send HTTP requests and obtain the return results of the API.

    $url = 'https://api.xiaokyo.com/wxyi/api/v1.0/sentence';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    Copy after login

    In this example, we use cURL's POST method to send the request, and URL-encode the request parameters before sending. Set CURLOPT_RETURNTRANSFER to true to save the return result of the API to the $response variable.

  5. Parse the return results
    After receiving the return results from the API, you can parse the return results and get the set of sentences you need.

    $result = json_decode($response, true);
    if ($result['status'] == 0) {
        $sentences = $result['data'];
        foreach ($sentences as $sentence) {
            echo $sentence['content'] . "
    ";
        }
    } else {
        echo '请求错误:' . $result['status'] . ' - ' . $result['msg'] . "
    ";
    }
    Copy after login

    First, we use the json_decode function to convert the JSON format string returned by the API into an associative array. Then, we determine whether the request was successful by checking the status field of the returned result. If successful, you can get the content of each sentence by looping through the sentences array.

Summary
Through the above steps, we can use PHP to connect to Baidu Wenxin Yiyan API and obtain the number of specific types of sentences by controlling the count parameter. By parsing the returned results, we can operate and display the obtained sentences.

I hope this article can help PHP developers who are developing API interfaces. If you have any questions or need further information, please consult the Baidu Wenxin Yiyan API documentation.

The above is the detailed content of PHP connects to Baidu Wenxin Yiyan API to obtain the number control method of specific types of sentences. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!