0

I am working with the Minecraft API and am attempting to decode JSON, and set a key as variable so that I can call it at another point. Here's my code that produces the error:

//grabbing the users information
if ($content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username))) {
    //do nothing
} else {
    $content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username) . '?at=0');
    $json = json_decode($content);

    foreach ($json as $currentName) {
        $input = $currentName->name;
    }

    $userSkin = "<img src='https://mcapi.ca/skin/3d/$currentName' />";
}

and here's the error:

Trying to get property of non-object in ... on line 31

Full PHP Code:

<?php

// Load the username from somewhere
if (
$username = $_POST["username"]
) {
    //do nothing 
} else {
    $username = "notch";
}


//allow the user to change the skin
$skinChange = "<a href='https://minecraft.net/profile/skin/remote?url=http://skins.minecraft.net/MinecraftSkins/$username.png' target='_blank' </a>";

//url to users 3D head (avatar)
$usersAvatar = "https://mcapi.ca/avatar/2d/$username/55";

//user's Avatar as favivon
$usersFavicon = "<link rel='shortcut icon' href='$usersAvatar' type='image/png' />";

//grabbing the users information
if ($content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username))
) {
  //do nothing
} else {
  $content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username) . '?at=0');
   $json = json_decode($content);

   foreach ($json as $currentName) {
    $input = $json['name'];
   }

   $userSkin = "<img src='https://mcapi.ca/skin/3d/$currentName' />";
}


// Decode it
$json = json_decode($content);

// Check for error
if (!empty($json->error)) {
    die('An error happened: ' . $json->errorMessage);
}

// Save the uuid
$uuid = $json->id;

// Get the history (using $json->uuid)
$content = file_get_contents('https://api.mojang.com/user/profiles/' . urlencode($uuid) . '/names');

// Decode it
$json = json_decode($content);

$names = array(); // Create a new array

foreach ($json as $name) {
    $input = $name->name;

    if (!empty($name->changedToAt)) {
        // Convert to YYYY-MM-DD HH:MM:SS format
        $time = date('Y-m-d H:i:s', $name->changedToAt);

        $input .= ' (changed at ' . $time . ')';
    }

    $names[] = $input; // Add each "name" value to our array "names"

}

//use $uuid tp grab UUID of the user - ---- - - - use $names to get name history of the user.



?>

Using var_dump($currentName); produces this output string(8) "_scrunch"

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
JLWillliamsUK
  • 185
  • 1
  • 3
  • 17
  • I am assuming line 31 is "$input = $currentName->name;" ? And if you can't see the error I would advise doing a couple php tutorials. This -> operator is used to access object properties. So to access array properties you do it array['key] = value. So $input = $json['name']; Look here how to access arrays with key value using forloops http://php.net/manual/en/control-structures.foreach.php – floor Mar 12 '15 at 14:35
  • 1
    Which line is line 31? – Theresa Mar 12 '15 at 14:36
  • what does `var_dump($json)` look like? – robbmj Mar 12 '15 at 14:36
  • So the problem is that, you don't understand the error message? You are trying to get a property of something, that is not an object. Hmmm. Can you see, where you are referring to an object? Maybe on line 31. You may want to know what is the type or value of our variable on line 31. Let's see. Maybe use `print_r` or `var_dump` to start debugging. – Krisztián Dudás Mar 12 '15 at 14:37
  • But seriously, the error is at `$currentName->name` check the value of `$currentName` or `$json` to find out some more. – Krisztián Dudás Mar 12 '15 at 14:38
  • @floor changing to that piece of code caused an error. `Cannot use object of type stdClass as array` – JLWillliamsUK Mar 12 '15 at 14:43
  • `var_dump($currentName)` to see what you're trying to work with. – deceze Mar 12 '15 at 14:45
  • It could be that you're getting a `204 no content` response, thus `json_decode()` is failing. For example: https://api.mojang.com/users/profiles/minecraft/harrydenley – ʰᵈˑ Mar 12 '15 at 14:45
  • @JLWillliamsUK I didn't give you a piece of code to work. I implied how it would look as an array. I gave you a link to the php manual page where there are examples that do exactly what you want. – floor Mar 12 '15 at 14:45
  • @JLWillliamsUK in the manual pages it says when you are using a key value for loop you simply just do $input = name; Note that after the loop finishes $input will only be holding the last value of the iteration. – floor Mar 12 '15 at 14:48
  • 1
    @deceze Although it could be marked as a duplicate, its not. OP may be getting a 204 as per the documentation "If there is no player with the given username an HTTP status code 204 (No Content) is sent without any HTTP body." http://wiki.vg/Mojang_API – ʰᵈˑ Mar 12 '15 at 14:48
  • @ʰᵈˑ Then `json_decode` should fail and it shouldn't even enter the loop. The exact error is still a duplicate. OP needs to at least debug why their object is not an object (as pointed to in many many dupes) and update the question accordingly. As it stands there could be any number of reasons. – deceze Mar 12 '15 at 15:20
  • @JLWillliamsUK So `$currentName` is "_scrunch", great. That's your string already. No need to do `->name` or anything at all. Just use `$currentName`. – deceze Mar 12 '15 at 15:22
  • @jonrsharpe: Just edit the typo instead of commenting. – Jonathan Hall Jan 22 '19 at 08:17

2 Answers2

2

Issue #1 - No user found

As per the documentation;

If there is no player with the given username an HTTP status code 204 (No Content) is sent without any HTTP body.

This means you'll be doing json_decode(); invalid json string - which will result in $json not being an object, hence your error.

You should change your code to become

$content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username) . '?at=0');
if( $http_response_header['0'] == "HTTP/1.1 204 No Content") {
    echo "no user";
    die;
}
$json = json_decode($content);

Issue #2 - Your foreach

As you're looping around the single dimensional object, $currentName becomes the value (a string) and not an object. Either do;

foreach ($json as $currentName) {
   echo $currentName . PHP_EOL; //Echo id, then echo name
}

Or access the properties directly

echo 'Id: '. $json->id;
echo '<br />';
echo 'Name: '. $json->name;
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
0

Using var_dump($currentName); produces this output string(8) "_scrunch"

So, just as the error message says, $currentName is not an object. It's a string. It's the string you're looking for. You don't need to do ->name on it, you can just use it as is.

deceze
  • 510,633
  • 85
  • 743
  • 889