簡體   English   中英

PHP 將 Roblox 頭像設置為圖像

[英]PHP Set Roblox Avatar to Image

我一直在嘗試解決這個問題大約 3 個小時,但一無所獲。 我正在嘗試將頭像 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 我一直在嘗試一堆不同的東西,但沒有任何效果,只是出現了一堆錯誤。

這是數據的樣子:

brenda12322323
eunaodoaisnudhnac
Lovesroblox1334
Cho5963
Sinmin191
vikaa_qwixxk
yourfav_stepsister
SnipesG0D

這是 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>';
}
?>

我正在嘗試將 $id 變量設置為用戶 ID,將頭像變量設置為圖像 URL

您需要首先從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)); 
}

該腳本執行兩次,第一次從names.txt中讀取名稱,並獲取 IDS,保存到users.json文件中。 然后當你再次執行它時,它會從 JSON 文件中獲取 ID,然后調用頭像,然后將它們重新組合在一起。 結果是一個 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
    }
}

然后給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>';
}

給你這樣的東西: Roblox 用戶名和個人資料圖片

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM