如何使用 PHP 在 JSON Twitter V2 响应中获取用户名?

How to grab the username in the JSON Twitter V2 response using PHP?

例如,我可以使用下面的代码获取“文本”,但是我如何获取 includes > user 下的用户名“Lionek_Gaming”?

$tweets = json_decode($response);

foreach($tweets->data as $tweet) {

$text = $tweet->text;

echo $text;

}

Json 回应

{
    "data": [
        {
            "author_id": "1169144412171055104",
            "id": "1386731275160072192",
            "text": "sell Bitcoin at sweet rates"
        }
 
    ],
    "includes": {
        "users": [

            {
                "id": "1304731585112158209",
                "name": "Lionek_official #BRG",
                "username": "Lionek_Gaming"
            }
        ]
    },

}

尝试以下操作:

foreach($tweets->includes->users as $user) {
    $username = $user->username;
    echo $username;
}