URL API (JSon) 存储在 MySql

URL API (JSon) store in MySql

我想在数据库中存储 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'};

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

对不起,我的编程很糟糕,我正在努力学习它。

试着 json_decode 你的结果是这样的:

<?php
  $account_id = 1122;

  $api = "http://url&account_id";

  $json = file_get_contents($api);
  
  //now you have result as associative array
  $result = json_decode($json, true);

  //print_r($result); 

  $nickname = $result['data'][$account_id]['nickname'];
?>

试着告诉我什么

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

创建 URL
  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>
        

  1. 一个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
  }

  
?>