为什么file_get_conents始终不起作用?

时间:2014-04-12 15:10:05

标签: php api

我正在编写一个应用程序,它将使用api从Minecraft帐户获取数据。 有时它返回数据,但有时它什么都不返回。有什么想法吗?

以下代码。

form.php的

<form action="info.php" method="post">
    <table>
        <tr>
            <td>Minecraft Username: </td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Get UUID!"></td>
        </tr>
    </table>
</form>

info.php的

<?php
  $uuid = "";
  $premium = "";
  $migrated = "";
  if(!(isset($_POST['name']))) {
    include('form.php');
    echo "Select A Username Please!";
  }else {
    $username = $_POST['name'];
    echo "<table>";
    echo "<tr><td>Username: </td><td>$username</td></tr>";
    flush();
    if($username == "Herobrine") {
        $uuid     = "Error.";
        echo "<tr><td>UUID: </td><td>$uuid</td></tr>";
        flush();
        $migrated = "Error.";
        echo "<tr><td>Migrated: </td><td>$migrated</td></tr>";
        flush();
        $premium  = "Error.";
        echo "<tr><td>Premium: </td><td>$premium</td></tr>";
        flush();
        $skin     = "javascript:alert('Error.');";
        echo "<tr><td>Skin: </td><td><a href=\"$skin\">Download</a></td></tr>";
        flush();
    }else {
        include_once("functions.php");
        $info = new Getter();
        $uuid     = $info->uuid($username);
        echo "<tr><td>UUID: </td><td>$uuid</td></tr>";
        $migrated = $info->migrated($username);
        echo "<tr><td>Migrated: </td><td>$migrated</td></tr>";
        $premium  = $info->premium($username);
        echo "<tr><td>Premium: </td><td>$premium</td></tr>";
        $skin     = $info->skin($username);
        echo "<tr><td>Skin: </td><td><a href=\"$skin\">Download</a></td></tr>";
    }   
  }
  echo "</table>";
?>

的functions.php

<?php
    class Getter {
        // Function To Get UUID
        function uuid($username) {
            // Get JSON Code
            $uuid = file_get_contents("http://theminecraftapi.com/v1/?get=uuid&user=$username");
            // Convert JSON to Array
            $uuid = json_decode($uuid, true);
            // Convert Array To String
             return $migrated['migrated'];
        }

        // Function To Get Migrated
        function migrated($username) {
            // Get JSON Code
            $migrated = file_get_contents("http://theminecraftapi.com/v1/?get=migrated&user=$username");
            // Convert JSON to Array
            $migrated = json_decode($migrated, true);
            // Convert Array to String
            return $migrated['migrated'];
        }

        // Function To Get Premium
        function premium($username) {
            // Get JSON Code
            $premium = file_get_contents("http://theminecraftapi.com/v1/?get=premium&user=$username");
            // Convert JSON To Array
            $premium = json_decode($premium, true);
            // Convert Array To String
            return $premium['premium'];
        }

        // Function To Get Skin
        function skin($username) {
            // Make Link
            return "http://skins.minecraft.net/MinecraftSkins/$username.png";
        }
    }
?>

我首先让它完美运行,但现在它有时只返回文件的实际内容。 :(为什么这不起作用?请帮忙!

它返回的内容: 用户名:KMCD00 UUID:
迁移:
保险费:
皮肤:Download

1 个答案:

答案 0 :(得分:0)

我发现它是9次,因为除非您提供用户代理,否则响应服务器不会响应。

file_get_contents("http://theminecraftapi.com/v1/?get=premium&user=$username"),false,stream_context_create(
    array("http" => array("user_agent" => "any"))
));