-1

I am writing an application that will use an api to get data from a Minecraft account. Sometimes it returns data, but sometimes it returns nothing. Any ideas?

Code Below.

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";
        }
    }
?>

I had it working perfectly at first but now it only sometimes returns what the files actually are. :( Why does this not work? Please help!

What it returns: Username: KMCD00 UUID:
Migrated:
Premium:
Skin: Download

  • 1
    Are you certain you always pass the correct username? Does it sometimes timeout? Does the API have daily or hourly limits you are exceeding? – Michael Berkowski Apr 12 '14 at 15:12
  • See [this question](http://stackoverflow.com/questions/15620124/http-requests-with-file-get-contents-getting-the-response-code) to learn how to inspect the HTTP response from `file_get_contents()`, which will help you to debug it. – Michael Berkowski Apr 12 '14 at 15:13
  • The api is fairly new, I am using the correct username and it does not have any limits or api keys. Get UUID: http://theminecraftapi.com/v1/?get=uuid&user=MCUSERNAME – user3478443 Apr 12 '14 at 15:13
  • I do not know if ByetHost allows cURL. Can anyone tell me how I can convert this to cURL to check? – user3478443 Apr 12 '14 at 15:15
  • Call phpinfo() and inspect the installed extensions to look for curl. But also check the httpresponse as in the linked question first. Don't jump to curl until you verify an issue with fgc – Michael Berkowski Apr 12 '14 at 15:36
  • SOrry, the Mojang official api was down at this time. I did not realize this. – user3478443 Apr 13 '14 at 01:50

1 Answers1

0

9 times out ten I find it's because the responding server doesn't respond unless you provide a user agent.

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