在对象中求值之后

时间:2018-12-14 18:59:41

标签: php

我在php中有这个对象。我必须从中获取值:标题,描述,图像和播放器。

    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 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/>";
    }

但是我有错误:

    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

我怀疑我的foreach函数存在错误。 抱歉,我是php的初学者。我需要此代码中的解决方案来学习。

如何维修?

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

尝试一下:

echo $player["snippet"]["description"];

答案 1 :(得分:0)

在这种情况下,您不需要使用foreach。只需直接获取属性,如下所示:

$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;