저는 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"
에 따라 문서 ;
주어진 사용자 이름을 가진 플레이어가 없으면 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);
1 차원 개체를 반복 할 때 개체 $currentName
가 아닌 값 (문자열)이됩니다. 둘 중 하나를 수행하십시오.
foreach ($json as $currentName) {
echo $currentName . PHP_EOL; //Echo id, then echo name
}
또는 속성에 직접 액세스
echo 'Id: '. $json->id;
echo '<br />';
echo 'Name: '. $json->name;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다