尝试使用Minecraft API获取非对象的属性

时间:2015-03-12 14:30:30

标签: php json

我正在使用Minecraft API并尝试解码JSON,并将一个键设置为变量,以便我可以在另一个点调用它。这是产生错误的代码:

//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' />";
}

这是错误:

  

尝试在第31行的......中获取非对象的属性

完整的PHP代码:

<?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.



?>

使用var_dump($currentName);生成此输出string(8) "_scrunch"

2 个答案:

答案 0 :(得分:2)

问题#1 - 找不到用户

根据the documentation;

  

如果没有具有给定用户名的播放器,则在没有任何HTTP正文的情况下发送HTTP状态代码204(无内容)

这意味着您将执行json_decode();无效的json字符串 - 这将导致$json不是对象,因此您的错误。

您应该将代码更改为

$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);

问题#2 - 您的foreach

当你绕着单维对象循环时,$currentName变成值(字符串)而不是对象。要么这样做;

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

或直接访问属性

echo 'Id: '. $json->id;
echo '<br />';
echo 'Name: '. $json->name;

答案 1 :(得分:0)

  

使用var_dump($currentName);生成此输出string(8) "_scrunch"

因此,正如错误消息所述,$currentName不是对象。这是一个字符串。这是你正在寻找的字符串。您不需要对其进行->name,您可以按原样使用它。