2015-04-15 40 views
1

我想製作一個「if」來檢查我的YouTube播放列表是否爲空,我已經看到有很多不同類型的youtube-api。 我想知道是否有人會幫我做一個if if檢查我的youtube-platlist中是否有視頻。檢查YouTube播放列表是否爲空

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

回答

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> 

+0

[只有鏈路答案氣餒](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good -answers) –