Search code examples
phpjsontwitter

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


I can grab "text" for example with the code below, but how can I grab the username "Lionek_Gaming" under includes > user ?

$tweets = json_decode($response);

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

$text = $tweet->text;

echo $text;

}

Json response

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

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

}

Solution

  • Try the following:

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