检查youtube播放列表是否为空

时间:2015-04-15 11:38:03

标签: php youtube-data-api

我想做一个" if"要检查我的youtube播放列表是否为空,我发现有很多不同类型的youtube-api。 我想知道是否有人会帮我制作一个if来检查我的youtube-platlist中是否有视频。

$playlist = youtubeplaylist function;
if ($playlist is empty){
    echo "Playlist is empty";
}
else{ 
    echo "Playlist is not empty";
}

1 个答案:

答案 0 :(得分:0)

我在这段代码上使用了代码并进行了一些编辑,`

<div class="content-wrapper">

    <?php
    //define playlist ID
    $playlistID = '8BCDD04DE8F771B2';
    //set feed URL
    $feedURL = 'https://gdata.youtube.com/feeds/api/playlists/'.$playlistID.'?v=2';
    //turn feed into simpleXML object
    $sxml = simplexml_load_file($feedURL);
    /*
    //If simplexml_load_file() isn't supported by your server,
    //you can alternatively use PHP cURL to parse through the XML
    function load_file_from_url($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_REFERER, 'http://www.yourdomain.com');
        $str = curl_exec($curl);
        curl_close($curl);
        return $str;
    }
    function load_xml_from_url($url) {
        return simplexml_load_string(load_file_from_url($url));
    }
    $sxml = load_xml_from_url($feedURL);
    */
    ?>

    <?php foreach($sxml -> entry as $entry) : ?>

        <?php
        //get author
        $author = $entry->author->name;
        //get namespaces in entry node
        $namespaces = $entry->getNameSpaces(true);
        //get children nodes in media namespace
        $media = $entry->children($namespaces['media']);
        //get title
        $title = $media->group->title;
        //get description
        $description = $media->group->description;
        //get video player URL
        $player_attrs = $media->group->player->attributes();
        $player_url = $player_attrs['url'];
        //get video thumbnail
        $thumb_attrs = $media->group->thumbnail[0]->attributes();
        //get children nodes of yt namespace in media namespace
        $yt = $media->children($namespaces['yt']);
        //get duration
        $duration_attrs = $yt->duration->attributes();
        $length = floor($duration_attrs['seconds'] / 60).':'.$duration_attrs['seconds'] % 60;
        //get children nodes of yt namespace in entry
        $yt = $entry->children($namespaces['yt']);
        //get view count
        $stats_attrs = $yt->statistics->attributes();
        $viewCount = $stats_attrs['viewCount'];
        //get children nodes of gd namespace
        $gd = $entry->children($namespaces['gd']);
        //if rating exists
        if ($gd->rating) {
            //get and set rating
            $attrs = $gd->rating->attributes();
            $rating = $attrs['average'];
        } else {
            //otherwise set rating to 0
            $rating = 0;
        }
        //get video link
        $link = $entry->link[0]->attributes();
        $embedLink = $link['href'];
        //use video link to format embed link
        $embedLink = str_replace("&feature=youtube_gdata", "", $embedLink);
        $embedLink = str_replace("/watch?v=", "/embed/", $embedLink);
        ?>

        <div class="post-wrapper">

            <iframe width="400" height="315" src="<?= $embedLink; ?>" frameborder="0" allowfullscreen></iframe>

            <h2><?= $title; ?></h2>

            <p><strong>Description</strong>
            <br /><?= $description; ?></p>

            <p><strong>Author</strong>
            <br /><?= $author; ?></p>

            <p><strong>Length</strong>
            <br /><?= $length; ?></p>

            <p><strong>Views</strong>
            <br /><?= $viewCount; ?></p>

            <p><strong>Thumbnail</strong>
            <br /><img src="<?= $thumb_attrs; ?>" alt="Video Thumbnail" /></p>

        </div>

    <?php endforeach; ?>

</div>