2013-07-18 83 views
4

我知道我可以通過播放列表來循環播放列表來比較播放列表是否存在於YouTube上,但是這是兩個電話(下面的例子),因爲我已經知道播放列表ID,我應該只能打一個電話。通過播放列表YouTube API v3:如何從PlaylistItems中返回404(未找到)錯誤?

兩個調用到YouTube環路(不是我想要做的方式):

# Playlist ID from URL 
$playlist=$_GET['playlist']; 

# Playlists from YoUtube 
$playlists=$yt->PlaylistsListFeed(); 

# Loop through the playlists. 
foreach($playlists as $playlist_feed) 
{ 
    if($playlist_feed['id']==$playlist) 
    { 
     # This is a playlist 
     $this->setIsPlaylist(TRUE); 

     # Assign the Playlist Items to a variable. 
     $video_search=$yt->PlaylistItems($playlist_feed['id']); 
     break; 
    } 
} 

# If $video_search is not set then playlist does not exist. 
if(!isset($video_search)) 
{ 
    return $display='That playlist doesn\'t exist. Please choose another.'; 
} 

我的代碼爲單電工作,但我無法弄清楚如何獲得如果播放列表ID不存在,則返回404錯誤(下面的YouTube結果)。

YouTube的方法(PlaylistItems):

/** 
* PlaylistItems 
* 
* Retrieves videos from a playlist. 
* 
* @param int $playlist_id   The ID of the playlist. 
* @param string $part    The part parameter specifies a comma-separated list of 
*           one or more playlistItem resource properties that the API 
*           response will include. The part names that you can include 
*           in the parameter value are id, snippet, and contentDetails. 
* @access public 
*/ 
public function PlaylistItems($playlist_id, $part='snippet') 
{ 
    # Set the Database instance to a variable. 
    $doc=Document::getInstance(); 
    try 
    { 
     # Bring $youtube_service into the scope 
     global $youtube_service; 

     $playlistsItems_optParams=array('maxResults' => 50, 'playlistId' => $playlist_id); 
     $playlistsItems=$youtube_service->playlistItems->listPlaylistItems($part, $playlistsItems_optParams); 

     $new_array=$this->rebuildArray($playlistsItems, TRUE); 

     # Instantiate a new Utility object. 
     $utility=new Utility(); 
     # Sort the playlist array by date. 
     $items=$utility->sortByDate($new_array, 'publishedAt'); 

     return $items; 
    } 
    catch(Exception $e) 
    { 
     # Print Results (below) 
     print_r($e); exit; 

     if($e->getCode()==404) 
     { 
      $doc->setError('That playlist doesn\'t exist. Please choose another.'); 
     } 
    } 
} #==== End -- PlaylistItems 

單呼代碼:

# Playlist ID from URL 
$playlist=$_GET['playlist']; 

# Assign the Playlist Items to a variable. 
$video_search=$yt->PlaylistItems($playlist); 

返回從PlaylistItems方法

Google_ServiceException Object 
(
    [errors:protected] => Array 
     (
      [0] => Array 
       (
        [domain] => youtube.playlistItem 
        [reason] => playlistNotFound 
        [message] => Not Found 
        [locationType] => parameter 
        [location] => playlistId 
       ) 

     ) 

    [message:protected] => Error calling GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=PLD2D6E64D9CD49E1&key=AIzaSyDVGJhk7DfKKJAaciX14aqyw_YbPaT4fp4: (404) Not Found 
    [string:Exception:private] => 
    [code:protected] => 404 
    [file:protected] => /Volumes/Coding/WebDev/CWIS/cwis.org/application/modules/Social/Google/io/Google_REST.php 
    [line:protected] => 66 
    [trace:Exception:private] => Array 
     (
      [0] => Array 
       (
        [file] => /Volumes/Coding/WebDev/CWIS/cwis.org/application/modules/Social/Google/io/Google_REST.php 
        [line] => 36 
        [function] => decodeHttpResponse 
        [class] => Google_REST 
        [type] => :: 
        [args] => Array 
         (
          [0] => Google_HttpRequest Object 
           (
            [batchHeaders:Google_HttpRequest:private] => Array 
             (
              [Content-Type] => application/http 
              [Content-Transfer-Encoding] => binary 
              [MIME-Version] => 1.0 
              [Content-Length] => 
             ) 

            [url:protected] => https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=PLD2D6E64D9CD49E1&key=AIzaSyDVGJhk7DfKKJAaciX14aqyw_YbPaT4fp4 
            [requestMethod:protected] => GET 
            [requestHeaders:protected] => Array 
             (
             ) 

            [postBody:protected] => 
            [userAgent:protected] => google-api-php-client/0.6.0 
            [responseHttpCode:protected] => 404 
            [responseHeaders:protected] => Array 
             (
              [content-type] => application/json; charset=UTF-8 
              [date] => Thu, 18 Jul 2013 05:11:11 GMT 
              [expires] => Thu, 18 Jul 2013 05:11:11 GMT 
              [cache-control] => private, max-age=0 
              [x-content-type-options] => nosniff 
              [x-frame-options] => SAMEORIGIN 
              [x-xss-protection] => 1; mode=block 
              [server] => GSE 
              [transfer-encoding] => chunked 
             ) 

            [responseBody:protected] => { 
"error": { 
    "errors": [ 
    { 
    "domain": "youtube.playlistItem", 
    "reason": "playlistNotFound", 
    "message": "Not Found", 
    "locationType": "parameter", 
    "location": "playlistId" 
    } 
    ], 
    "code": 404, 
    "message": "Not Found" 
} 
} 

            [accessKey] => 
           ) 

         ) 

       ) 

      [1] => Array 
       (
        [file] => /Volumes/Coding/WebDev/CWIS/cwis.org/application/modules/Social/Google/service/Google_ServiceResource.php 
        [line] => 186 
        [function] => execute 
        [class] => Google_REST 
        [type] => :: 
        [args] => Array 
         (
          [0] => Google_HttpRequest Object 
           (
            [batchHeaders:Google_HttpRequest:private] => Array 
             (
              [Content-Type] => application/http 
              [Content-Transfer-Encoding] => binary 
              [MIME-Version] => 1.0 
              [Content-Length] => 
             ) 

            [url:protected] => https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=PLD2D6E64D9CD49E1&key=AIzaSyDVGJhk7DfKKJAaciX14aqyw_YbPaT4fp4 
            [requestMethod:protected] => GET 
            [requestHeaders:protected] => Array 
             (
             ) 

            [postBody:protected] => 
            [userAgent:protected] => google-api-php-client/0.6.0 
            [responseHttpCode:protected] => 404 
            [responseHeaders:protected] => Array 
             (
              [content-type] => application/json; charset=UTF-8 
              [date] => Thu, 18 Jul 2013 05:11:11 GMT 
              [expires] => Thu, 18 Jul 2013 05:11:11 GMT 
              [cache-control] => private, max-age=0 
              [x-content-type-options] => nosniff 
              [x-frame-options] => SAMEORIGIN 
              [x-xss-protection] => 1; mode=block 
              [server] => GSE 
              [transfer-encoding] => chunked 
             ) 

            [responseBody:protected] => { 
"error": { 
    "errors": [ 
    { 
    "domain": "youtube.playlistItem", 
    "reason": "playlistNotFound", 
    "message": "Not Found", 
    "locationType": "parameter", 
    "location": "playlistId" 
    } 
    ], 
    "code": 404, 
    "message": "Not Found" 
} 
} 

            [accessKey] => 
           ) 

         ) 

       ) 

      [2] => Array 
       (
        [file] => /Volumes/Coding/WebDev/CWIS/cwis.org/application/modules/Social/Google/contrib/Google_YoutubeService.php 
        [line] => 425 
        [function] => __call 
        [class] => Google_ServiceResource 
        [type] => -> 
        [args] => Array 
         (
          [0] => list 
          [1] => Array 
           (
            [0] => Array 
             (
              [part] => snippet 
              [maxResults] => 50 
              [playlistId] => PLD2D6E64D9CD49E1 
             ) 

           ) 

         ) 

       ) 

      [3] => Array 
       (
        [file] => /Volumes/Coding/WebDev/CWIS/cwis.org/application/modules/Media/YouTube.php 
        [line] => 412 
        [function] => listPlaylistItems 
        [class] => Google_PlaylistItemsServiceResource 
        [type] => -> 
        [args] => Array 
         (
          [0] => snippet 
          [1] => Array 
           (
            [maxResults] => 50 
            [playlistId] => PLD2D6E64D9CD49E1 
           ) 

         ) 

       ) 

      [4] => Array 
       (
        [file] => /Volumes/Coding/WebDev/CWIS/cwis.org/application/modules/Media/Video.php 
        [line] => 1130 
        [function] => PlaylistItems 
        [class] => YouTube 
        [type] => -> 
        [args] => Array 
         (
          [0] => PLD2D6E64D9CD49E1 
         ) 

       ) 

      [5] => Array 
       (
        [file] => /Volumes/Coding/WebDev/CWIS/cwis.org/public/media/video/index.php 
        [line] => 59 
        [function] => displayVideoFeed 
        [class] => Video 
        [type] => -> 
        [args] => Array 
         (
         ) 

       ) 

     ) 

    [previous:Exception:private] => 
) 

正如你一個看到的一切要麼是protectedprivate所以我怎麼想的知道是否有錯誤?

回答

0

我最終列出了YouTube上的視頻並獲取其狀態。 作爲cron腳本運行,每10分鐘一次。

# Get new uploaded videos from the database. 
$get_new_videos=$db->get_results('SELECT `id`, `file_name`, `api` FROM `'.DBPREFIX.'videos` WHERE `new` IS NULL'); 

# If there are new videos... 
if($get_new_videos>0) 
{ 
    # Loop through the new videos 
    foreach($get_new_videos as $new_video) 
    { 
     # Decode the `api` field. 
     $api_decoded=json_decode($new_video->api); 
     # Get the YouTube Video ID. 
     $video_yt_id=$api_decoded->youtube_id; 

     if(isset($new_video->file_name)) 
     { 
      # Set the path to the video on the server. 
      $video_path='videos'.DS.$new_video->file_name; 
     } 

     $to=TO_USER; 
     $reply_to=FROM_SERVER; 
     $subject="Video status from ".DOMAIN_NAME; 
     $body=''; 

     # Check the video status. 
     $check_status=$yt->listVideos('status', array('id' => $video_yt_id)); 

     # Did YouTube return results? 
     if(!empty($check_status['items'])) 
     { 
      # Has the video been processed? Default is TRUE. will be changed to FALSE if the video still has "uploaded" status. 
      $processed=TRUE; 

      # Loop through the videos from YouTube. 
      foreach($check_status['items'] as $status) 
      { 
       if($status['status']['uploadStatus']=="uploaded") 
       { 
        # The video has not been processed yet so do not send an email. 
        $processed=FALSE; 
       } 
       # Check to see if the YouTube upload was a success. 
       elseif($status['status']['uploadStatus']=="processed") 
       { 
        # Tell the user the video was uploaded. 
        $body.='Your video has been uploaded to YouTube and can be viewed at http://'.FULL_DOMAIN.'media/videos/?video='.$new_video->id; 
       } 
       # Check if the uploaded video status is rejected. 
       elseif($status['status']['uploadStatus']=="rejected") 
       { 
        if(isset($new_video->file_name)) 
        { 
         # Get the Upload class. 
         require_once MODULES.'Form'.DS.'Upload.php'; 
         # Instantiate an Upload object. 
         $upload=new Upload($video_path); 
         # Delete video file from server. 
         $upload->deleteFile($video_path); 
         # Remove extention from filename. 
         $name_no_ext=substr($new_video->file_name, 0, strrpos($new_video->file_name, '.')); 
         # Delete the thumbnails from the server. 
         $upload->deleteFile(IMAGES_PATH.'videos'.DS.'original'.DS.$name_no_ext.'.jpg'); 
         $upload->deleteFile(IMAGES_PATH.'videos'.DS.$name_no_ext.'*.jpg', TRUE); 

         # Delete rejected video from YouTube 
         $delete_response=$yt->deleteVideo($video_yt_id); 
        } 

        # Need to delete the entry from the database as well. 
        $db->query('DELETE FROM `'.DBPREFIX.'videos` WHERE `id` = '.$db->quote($new_video->id).' LIMIT 1'); 

        # Check if the rejection status was a duplicate. 
        if($status['status']['rejectionReason']=="duplicate") 
        { 
         # Tell the user the video was a duplicate. 
         $body.='Your video was rejected because it was a duplicate video'; 
        } 
       } 
      } 
     } 
     else 
     { 
      $body.='Your video was not found on YouTube'; 
      $processed=TRUE; 
     } 

     # Update database if the video has been "processed". 
     if($processed===TRUE) 
     { 
      # Get the PHPMailer class. 
      require_once MODULES.'Email'.DS.'Email.php'; 
      # Instantiate a new PHPMailer object. 
      $mail=new Email(); 

      $mail->sendEmail($subject, $to, $body, $reply_to); 

      # Set new videos to old. 
      $db->query('UPDATE `'.DBPREFIX.'videos` SET `new` = 0 WHERE `id` = '.$db->quote($new_video->id).' LIMIT 1'); 
     } 
    } 
} 
0

如果您使用的是zend google api庫,那麼獲取播放列表提要非常容易,並且可以捕獲任何如果它不存在的情況下將返回的錯誤。最簡單的解決方案是獲取帳戶中所有播放列表的列表,並循環播放以查看播放列表ID是否與您擁有的ID匹配,然後從那裏獲取播放列表條目。這樣,真的不會有任何錯誤或例外。但是,這是多次打電話,你試圖避免。爲了進行一次通話,您需要擁有播放列表供稿網址,然後進行一次通話並捕獲異常。這些是我在使用YouTube播放列表時使用的幾項功能的片段。

選項1:

$this->api = new Zend_Gdata_YouTube(); 
$this->api->setMajorProtocolVersion(2); 

$feed = $this->api->retrieveAllEntriesForFeed($this->api->getPlaylistListFeed('default')); 
$playlistEntry = $this->__getPlaylistEntryFromFeed($feed, $playlistID); 
//do what you want with the playlist entry, like get the list of all the videos 




function __getPlaylistEntryFromFeed($feed, $entryId) { 
    foreach($feed as $entry) { 
     if($entry->getPlaylistId()->text == $entryId) { 
     return $entry; 
     } 
    } 
} 

選項2:

try { 
    $playlistVideoFeed = $this->api->getPlaylistVideoFeed($playlistFeedUrl. '?v=2&start-index=1'); 
    //this returns a list of videos in the playlist that you can loop through. see example function below. 
} catch (Zend_Gdata_App_Exception $e) { 
    echo $e->getMessage(); 
} 


function getPlaylistVideos($playlistFeedUrl, $start=1, $total=0, $videos = array()) { 
     $playlistVideoFeed = $this->api->getPlaylistVideoFeed($playlistFeedUrl. '?v=2&start-index='.$start); 

     foreach ($playlistVideoFeed as $videoEntry) { 
      $videos[] = $this->getDataFromVideoEntry($videoEntry); 
     } 
     $count = count($playlistVideoFeed); 

     if(count($videos) < $total && $count != 0) { 
      $videos = $this->getPlaylistvideos($playlistFeedUrl, $start + ($count-1), $total, $videos); 
     } 
     return $videos; 
    } 

採取這些例子和你的代碼中使用它們,你可以試試這個樣子:

# Playlist ID from URL 
$playlist=$_GET['playlist']; 

# Assign the Playlist Items to a variable. 
$video_search; 
try { 
    $video_search = $yt->PlaylistItems($playlist); 
} catch(Google_ServiceException $e) { 
    //there was an error 
    echo $e->getMessage(); 
} 

希望有所幫助。