【问题标题】:Keep getting error from php and mysql不断从 php 和 mysql 收到错误
【发布时间】:2017-06-04 02:26:44
【问题描述】:

我正在尝试创建一个网站,该网站从我的世界服务器创建的数据库中获取统计信息。我在将 uuid 插入 mojangs API 并作为用户名出现时遇到问题。当您第一次访问该站点时它可以工作,但是当您刷新它时我收到错误:

Warning: file_get_contents(https://sessionserver.mojang.com/session/minecraft/profile/07eb744f35b74ac083e90e0377a5a967): failed to open stream: HTTP request failed! HTTP/1.1 429 429 in C:\xampp\htdocs\index.php on line 47
Notice: Trying to get property of non-object in C:\xampp\htdocs\index.php on line 49
Warning: file_get_contents(https://sessionserver.mojang.com/session/minecraft/profile/f5cf9ff1b9a74176983bb8b1d0c0ee70): failed to open stream: HTTP request failed! HTTP/1.1 429 429 in C:\xampp\htdocs\index.php on line 47
Notice: Trying to get property of non-object in C:\xampp\htdocs\index.php on line 49

在我刷新它之后,名称从表格中消失了,它给了我这些错误。我真的需要尽快解决这个问题!

<code>
<!DOCTYPE html>
<html>
<head>
<style>
body {
    font-family: arial;
    background-color: #3be5de;
}
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
    text-align: center;
}
</style>
<title>SuperBlaze27 Players-Server Stats</title>
<!--<link>-->
</head>
<body>
<div style="font-family: arial; color: #23a09b;">
<h1>SuperBlaze27 Minecraft Server Player Stats</h1>
<h3>Player joins</h3>
</div>
<?php
$servername = "localhost";
$username = "stats";
$password = "***";
$dbname = "stats3";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM `stats3_joins`";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
echo "<table><tr><th>Uniqe Player ID</th><th>Username</th><th>Player Head</th><th>Times Joined</th></tr>

";
    while($row = $result->fetch_assoc()) {
$uuid = $row["uuid"];
$uuid = str_replace('-', '', $uuid);
$json_response = file_get_contents("https://sessionserver.mojang.com/session/minecraft/profile/" . $uuid . "");
$data = json_decode($json_response);
        print "<tr><td>#" . $row["id"]. "</td><td> ". $data->name . "</td><td><img src='https://crafatar.com/avatars/" . $uuid . "?size=32'/img></td><td>" . $row["value"]. "</td></tr>";
    }
} else {
    echo "0 results";
}
$conn->close();
        echo "</table>";


// echo "Player name:".$data->name;

?>

</body>
</html>

顺便说一句,我用 *** 替换了密码,但在真正的密码中它可以工作,所以这不是问题。提前谢谢!

【问题讨论】:

  • 你能应用一致的格式吗?像 pastebin 这样的网站可以提供帮助。
  • 您确定$json_response = file_get_contents("https://sessionserver.mojang.com/session/minecraft/profile/" . $uuid . ""); 返回有效响应吗?
  • 您将其视为有效数据源而不检查其行为。
  • @mateusz-sip hmmmm 你是什么意思?我该如何解决?

标签: php html mysql json uuid


【解决方案1】:

429 HTTP 错误意味着您在一秒钟内向服务器发送了太多请求(在您的情况下为 sessionserver.mojang.com)。

您必须处理限制这一事实是意料之中的,因为从 MySQL 读取数据非常快,并且您的服务器每秒会发出许多请求以获取数据。您需要在查询之前添加延迟来解决此问题(这反过来会减慢页面加载速度)。因此,我也建议你将接收到的数据缓存起来,不要在每次请求页面时都重新下载。

【讨论】:

猜你喜欢
  • 2019-05-05
  • 1970-01-01
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
相关资源
最近更新 更多