Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

masandikdev's avatar

Youtube API v3 PHP Objects structure

Hi all, i just develop new site with Youtube API v3 im using wrapper

https://packagist.org/packages/alaouy/youtube

i have problem that by default its return PHP Objects instead of Array structure, that be problem when i try to return it. because have stdClass and with index key of array. i usually make simple json_decode($arrays, true) to make it work. but its return PHP objects by default.

anyone can help me how to return it by using foreach loop. here my Controller

$params = array(
            'q'             => 'Taylor Swift',
            'type'          => 'video',
            'part'          => 'id, snippet',
            'maxResults'    => 10
 );

$videos     = Youtube::searchAdvanced($params, true);

what im trying right now in views is:

@foreach ($videos->results as $value)
    {{ $value->snippet->title }}
@endforeach

PHP Objects structure

{
    "results": [
    {
        "kind": "youtube#searchResult",
        "etag": "\"jOXstHOM20qemPbHbyzf7ztZ7rI/76bB--radvqm6aBPLUNtCV7C5Jk\"",
        "id": {
            "kind": "youtube#video",
            "videoId": "IdneKLhsWOQ"
        },
        "snippet": {
            "publishedAt": "2015-08-31T00:55:00.000Z",
            "channelId": "UCANLZYMidaCbLQFWXBC95Jg",
            "title": "Taylor Swift - Wildest Dreams",
            "description": "Check out Taylor's new video “Wildest Dreams”. “Wildest Dreams” is Available Now on her multi-platinum release 1989 on iTunes or Google Play: ...",
            "thumbnails": {
                "default": {
                    "url": "https://i.ytimg.com/vi/IdneKLhsWOQ/default.jpg"
                },
                "medium": {
                    "url": "https://i.ytimg.com/vi/IdneKLhsWOQ/mqdefault.jpg"
                },
                "high": {
                    "url": "https://i.ytimg.com/vi/IdneKLhsWOQ/hqdefault.jpg"
                }
            },
            "channelTitle": "TaylorSwiftVEVO",
            "liveBroadcastContent": "none"
        }
    },
}

with dd result:

array:2 [▼
  "results" => array:10 [▼
    0 => {#158 ▼
      +"kind": "youtube#searchResult"
      +"etag": ""jOXstHOM20qemPbHbyzf7ztZ7rI/76bB--radvqm6aBPLUNtCV7C5Jk""
      +"id": {#162 ▶}
      +"snippet": {#163 ▼
        +"publishedAt": "2015-08-31T00:55:00.000Z"
        +"channelId": "UCANLZYMidaCbLQFWXBC95Jg"
        +"title": "Taylor Swift - Wildest Dreams"
        +"description": "Check out Taylor's new video “Wildest Dreams”. “Wildest Dreams” is Available Now on her multi-platinum release 1989 on iTunes or Google Play: ..."
        +"thumbnails": {#164 ▶}
        +"channelTitle": "TaylorSwiftVEVO"
        +"liveBroadcastContent": "none"
      }
    }
    1 => {#168 ▶}
    2 => {#175 ▶}
    3 => {#182 ▶}
    4 => {#189 ▶}
    5 => {#196 ▶}
    6 => {#203 ▶}
    7 => {#210 ▶}
    8 => {#217 ▶}
    9 => {#224 ▶}
  ]
  "info" => array:6 [▶]
]

anyone can help me how to return it on the right way. thanks

0 likes
3 replies
maquin's avatar

Hello masandikdev, I have the same problem ... did it solve ?

masandikdev's avatar
masandikdev
OP
Best Answer
Level 1

@maquin it solved, and i forget to update this thread.

just use deafult array structure, try this:

@foreach ($videos['results'] as $value)
    {{ $value->snippet->title }}
@endforeach 

hope it help.

helpmyworld's avatar

@masandikdev

Can you share with me how you achieve the desired results from the beginning? I want to show youtube videos on my blog. I want to show just a thumbnail and allow users to proceed to watch videos on youtube.

Please or to participate in this conversation.