如何将 api 调用生成的响应存储到 wordpress 中的 mysql 数据库

How to store the response generated from an api call to the mysql database in wordpress

我刚开始了解编码语言,并开始使用 wordpress 学习东西。 我正在尝试集成一个支付网关并可以生成 access_token 和 refresh_token,我需要将它们存储到我的数据库中以便进一步 api 调用支付和用户数据更新。

以下是我 运行 生成令牌的代码。 请帮助了解如何将令牌值存储在数据库中。 我试过复制 wordpress 注册码来插入值,但是 无法成功。

    <?php
    session_start();
    include_once "page-signup.php";

    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => "https://test.instamojo.com/oauth2/token/",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS =>  
    "client_id=gC4z8rBV55SDiavZobpvZCcanwK3mbnY
    &client_secret=dYFbCXaX9WUy1c3kkCXV8JRJkTxmLcxCXwncVKWlWsh8c0QOI
    5Uz30PCzOieC879RT7PLsEwrRKZDvZXqYpF5fZiE2Z62z3dly7p7ZUbGHTmOWmBsh3
    &grant_type=password
    &username=$username
    &password=$password",
    CURLOPT_HTTPHEADER => array(
    "Cache-Control: no-cache",
    "Content-Type: application/x-www-form-urlencoded",
      ),
      ));
    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
    echo "cURL Error #:" . $err;
        } else {

    $json_decode=json_decode($response,true);
    $refresh_token=$json_decode['refresh_token'];
    $access_token=$json_decode['access_token'];
      }


    echo $response;


     ?>

从 wordpress 数据库存储和检索此信息的最佳方法可能是使用 Options API:只需一个命令,您就可以存储、检索和删除令牌。 通常 wordpress 选项使用 normalized array 但您也可以直接存储 json 标记或只是一个字符串。