mysql - URL API (JSon) 存储在 MySql 中

标签 mysql json api

我想将API结果存储在数据库中 我对这个 API php 编码很陌生,

    <?php
$account_id = 1122;
$api = "http://url&account_id";
$json = file_get_contents($api);
print_r($json);
?>

这是输出

{"status":"ok","count":1,"meta":{"count":1},"data":{"1122":{"statistics":{"all":{"wins":112,"losses":118},"xp":721},"nickname":"nickie"}}}

现在我想将数据存储在变量中,并希望在表格中显示。 为了将其保存在变量中,我正在使用此方法。

$nickname = $decoded->{'data'}->{$account_id}->{'nickname'};
$max_xp = $decoded->{'data'}->{$account_id}->{'statistics'}->{'xp'};

如何显示表格中的所有数据。或主体页面上的任何位置。

抱歉我的编程很糟糕,我正在努力学习它。

最佳答案

在这种情况下,如果我理解正确的话,有必要创建一个带有两个文本框的 HTML 表单,稍后您可以在 PHP 页面中发送此表单,该页面可以检索这些输入字段的值并创建 URL API:

  1. HTML 页面

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Your page title</title>
  </head>
  <body>
    <div>
      <form method="post" action="/your/php/file">
        <div>
          <label for="name" />
          <input type="text" name="username" id="name" />
        </div>
        <div>
          <label for="location" />
          <input type="text" name="location" id="location" />
        </div>
        <div>
          <button>Submit</button>
        </div>
      </form>
    </div>
  </body>
</html>
        

  • 一个 PHP 页面
  • <?php
      
    
      if (isset($_POST['username'], $_POST['location']) && !empty($_POST['username']) && !empty($_POST['location'])) {
        $username = htmlspecialchars($_POST['username']);
        $location = htmlspecialchars($_POST['location']);
    
        $account_id = 1122;
    
        $api = "http://url?account_id&username={$username}&location={$location}";
    
        $json = file_get_contents($api);
      
        //now you have result as associative array
        $result = json_decode($json, true);
    
        //you can do what you want now
      }
    
      
    ?>

    关于mysql - URL API (JSon) 存储在 MySql 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28799365/

    相关文章:

    json - 使用discord.py 将已存在的 json 文件作为嵌入发送

    ios - 使用 objectForKey 将 URL 从 JSON 转换为 UIImageView 的图像

    php - Webmatrix 显示空白页

    php - Laravel 播种错误 - 错误的表名

    r - 有没有一种简洁的方法来读取 JSON 字符串并将其转换为新变量?

    .net - .net 中是否有读取 XML 注释文档文件的 API

    c# - 使用 C# 实现 Catia V5 自动化

    MYSQL - 检测具有共同三列的行

    Mysql 提交有时需要几分钟才能完成

    c# 通过 rdp 扫描 api