简体   繁体   English

PHP 将 Roblox 头像设置为图像

[英]PHP Set Roblox Avatar to Image

I've been trying to figure this out for like 3 hours and am not getting anywhere.我一直在尝试解决这个问题大约 3 个小时,但一无所获。 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 .我正在尝试将头像 div 中的 img src 设置为 Roblox 用户的头像 URL,此端点为 https://thumbnails.roblox.com/v1/users/avatar-bust?userIds=3102777127&size=150x150&format=Png&isCircular=true但它需要用户名,而我的数据是用户名,因此我们必须使用此端点来获取 ID 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 代码

<?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我正在尝试将 $id 变量设置为用户 ID,将头像变量设置为图像 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:您需要首先从get-by-username端点获取所有 ID,将它们传递到 avatars 端点,然后将数据放回一起:

<?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.该脚本执行两次,第一次从names.txt中读取名称,并获取 IDS,保存到users.json文件中。 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.然后当你再次执行它时,它会从 JSON 文件中获取 ID,然后调用头像,然后将它们重新组合在一起。 The result is a JSON file like so:结果是一个 JSON 文件,如下所示:

{
    "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:然后给output这个网页上的信息:

$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>';
}

Gives you something like this:给你这样的东西: Roblox 用户名和个人资料图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM