I've been trying to figure this out for like 3 hours and am not getting anywhere. I'm trying to set the img src in the avatar div to the Roblox user's avatar URL with this endpoint https://thumbnails.roblox.com/v1/users/avatar-bust?userIds=3102777127&size=150x150&format=Png&isCircular=true but it requires usernames and my data is usernames so we have to use this endpoint to get IDs https://api.roblox.com/users/get-by-username?username=VOICECHAT22983 . I have been trying a bunch of different stuff and nothing is working and just getting a bunch of errors.
This is how the data looks:
brenda12322323
eunaodoaisnudhnac
Lovesroblox1334
Cho5963
Sinmin191
vikaa_qwixxk
yourfav_stepsister
SnipesG0D
This is the PHP Code
<?php
$lines = file('../txt/names.txt');
foreach ($lines as $line) {
echo '<div class="avatar">
<img src="'.$avatar.'" alt="">
<h1>' . $line . '</h1>
<a href="https://www.roblox.com/users/'.$id.'/profile">Profile</a>
</div>';
}
?>
I'm trying to set the $id variable as the user's ID and the avatar variable as the image URL
You need to get all the IDs first from the get-by-username
endpoint, pass those into the avatars endpoint, then put the data back together:
<?php
/*
Question Author: cdnAshton
Question Answerer: Jacob Mulquin
Question: PHP Set Roblox Avatar to Image
URL: https://stackoverflow.com/questions/74977675/php-set-roblox-avatar-to-image
Tags: php, roblox
*/
function get_id_by_username($name)
{
$url = 'https://api.roblox.com/users/get-by-username?username=' . $name;
echo 'getting ' . $url . PHP_EOL;
return json_decode(file_get_contents($url), true);
}
function get_avatar_urls($ids)
{
$ids_param = implode(',', $ids);
$url = 'https://thumbnails.roblox.com/v1/users/avatar-bust?userIds='.$ids_param.'&size=150x150&format=Png&isCircular=true';
return json_decode(file_get_contents($url), true);
}
if (!file_exists('users.json')) {
$names = file('names.txt');
$users = [];
foreach ($names as $name) {
$name = trim($name);
$id = get_id_by_username($name);
$users[$id['Id']] = $id;
sleep(15); // Needed otherwise you will hit API rate-limits
}
file_put_contents('users.json', json_encode($ids, JSON_PRETTY_PRINT));
} else {
$users = json_decode(file_get_contents('users.json'), true);
$ids = [];
foreach ($users as $user) {
$ids[] = $user['Id'];
}
$avatars = get_avatar_urls($ids);
foreach ($avatars['data'] as $avatar) {
$users[$avatar['targetId']]['AvatarUri'] = $avatar['imageUrl'];
}
file_put_contents('users.json', json_encode($users, JSON_PRETTY_PRINT));
}
The script is executed twice, the first time it will read the names from names.txt
, and get the IDS, saving into a users.json
file. Then when you execute it again, it will get the IDs from the JSON file, then call the avatars and then merge it back together. The result is a JSON file like so:
{
"3528175625": {
"Id": 3528175625,
"Username": "brenda12322323",
"AvatarUri": "https:\/\/tr.rbxcdn.com\/3c195f46b44d37aa250c0d7c6ae41ded\/150\/150\/AvatarBust\/Png\/isCircular",
"AvatarFinal": false,
"IsOnline": false
},
"4028593775": {
"Id": 4028593775,
"Username": "eunaodoaisnudhnac",
"AvatarUri": "https:\/\/tr.rbxcdn.com\/0fc96af99739e29c780af459db6c2bd8\/150\/150\/AvatarBust\/Png\/isCircular",
"AvatarFinal": false,
"IsOnline": false
}
}
Then to output this information on a webpage:
$users = json_decode(file_get_contents('users.json'), true);
foreach ($users as $user) {
echo '<div class="avatar">
<img src="'.$user['AvatarUri'].'" alt="">
<h1>' . $user['Username'] . '</h1>
<a href="https://www.roblox.com/users/'.$user['Id'].'/profile">Profile</a>
</div>';
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.