dqy012345 2018-12-14 18:59
浏览 42
已采纳

Foreach在对象中的值之后

I have this object in php. I must get values from it: title, description, image and player.

    stdClass Object
(
    [kind] => youtube#video
    [etag] => "XI7nbFXulYBIpL0ayR_gDh3eu1k/-GCbscHL0lMNUZAFRWbHsd8S28w"
    [id] => QNNNL6PxmmM
    [snippet] => stdClass Object
        (
            [publishedAt] => 2017-07-26T08:43:49.000Z
            [channelId] => UCuX-ePmTx8oGhDQ8LKPPGuw
            [title] => Wielki Przejazd Rowerowy w duar  (11 czerwca 2017)
            [description] => Więcej informacji: http://duara.eu/2017/06/12/kolejny-rekord-wielkiego-przejazdu-rowerowego/
            [thumbnails] => stdClass Object
                (
                    [default] => stdClass Object
                        (
                            [url] => https://i.ytimg.com/vi/QNNNL6PxmmM/default.jpg
                            [width] => 120
                            [height] => 90
                        )

                    [medium] => stdClass Object
                        (
                            [url] => https://i.ytimg.com/vi/QNNNL6PxmmM/mqdefault.jpg
                            [width] => 320
                            [height] => 180
                        )

                    [high] => stdClass Object
                        (
                            [url] => https://i.ytimg.com/vi/QNNNL6PxmmM/hqdefault.jpg
                            [width] => 480
                            [height] => 360
                        )

                )

            [channelTitle] => Miasto duara
            [tags] => Array
                (
                    [0] => Wielki Przejazd Rowerowy w duar
                    [1] => Wielki Przejazd Rowerowy
                    [2] => Rowery
                    [3] => rowery duara
                    [4] => pasieczny
                )

            [categoryId] => 25
            [liveBroadcastContent] => none
            [localized] => stdClass Object
                (
                    [title] => Wielki Przejazd Rowerowy w duar  (11 czerwca 2017)
                    [description] => Więcej informacji: http://duara.eu/2017/06/12/kolejny-rekord-wielkiego-przejazdu-rowerowego/
                )

            [defaultAudioLanguage] => pl
        )

    [contentDetails] => stdClass Object
        (
            [duration] => PT1M50S
            [dimension] => 2d
            [definition] => hd
            [caption] => false
            [licensedContent] => 
            [projection] => rectangular
        )

    [status] => stdClass Object
        (
            [uploadStatus] => processed
            [privacyStatus] => public
            [license] => youtube
            [embeddable] => 1
            [publicStatsViewable] => 
        )

    [statistics] => stdClass Object
        (
            [viewCount] => 35
            [favoriteCount] => 0
            [commentCount] => 0
        )

    [player] => stdClass Object
        (
            [embedHtml] => <iframe width="480" height="270" src="//www.youtube.com/embed/QNNNL6PxmmM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
        )

)

I want print on screen from this object this values: title, description, image and player?

I try this code:


    showVideoDetails = $youtube->getVideoInfo('b2wvZf7ztxA'); 
    foreach ($showVideoDetails as $player) {
        echo "XX".$showVideoDetails->snippet->title . PHP_EOL."<br/>";
        echo $player->snippet->description . PHP_EOL."<br/>";
        echo $player->snippet->thumbnails->maxres->url . PHP_EOL."<br/>";
        echo $player->player . PHP_EOL."<br/><br/><br/>";
    }

but I have error:

    Notice: Trying to get property 'snippet' of non-object in /Applications/XAMPP/xamppfiles/htdocs/madcoda/index.php on line 29

Notice: Trying to get property 'description' of non-object in /Applications/XAMPP/xamppfiles/htdocs/madcoda/index.php on line 29


Notice: Trying to get property 'snippet' of non-object in /Applications/XAMPP/xamppfiles/htdocs/madcoda/index.php on line 30

Notice: Trying to get property 'thumbnails' of non-object in /Applications/XAMPP/xamppfiles/htdocs/madcoda/index.php on line 30

Notice: Trying to get property 'high' of non-object in /Applications/XAMPP/xamppfiles/htdocs/madcoda/index.php on line 30

Notice: Trying to get property 'url' of non-object in /Applications/XAMPP/xamppfiles/htdocs/madcoda/index.php on line 30


Notice: Trying to get property 'player' of non-object in /Applications/XAMPP/xamppfiles/htdocs/madcoda/index.php on line 31

I suspect there is an error in my foreach function. Sorry I'm beginner in php. I need solution in this code to learn.

How repair it?

Thank you for yours help

  • 写回答

2条回答 默认 最新

  • dongsui8162 2018-12-14 20:40
    关注

    In this case, you do not need to use foreach. Just get the properties directly as follows:

    $video = $youtube->getVideoInfo('b2wvZf7ztxA');
    
    echo <<<HTML
    {$video->snippet->title}<br/>
    {$video->snippet->description}<br/>
    {$video->snippet->thumbnails->high->url}<br/>
    {$video->player->embedHtml}<br/><br/><br/>
    HTML;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题