我想将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:
- 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/