title: YouTube API v3: How to get returned 404 (not found) error from PlaylistItems?
body: <p>I know I can loop through the playlists to compare if the playlist exists on Youtube but that is making two calls (example below) and since I already know the playlist ID I should just be able to make one call.</p>
  
  <p>Two calls to YouTube to loop through playlists (not the way I want to do it):</p>
  
  <pre><code># Playlist ID from URL
  $playlist=$_GET['playlist'];
  
  # Playlists from YoUtube
  $playlists=$yt-&gt;PlaylistsListFeed();
  
  # Loop through the playlists.
  foreach($playlists as $playlist_feed)
  {
      if($playlist_feed['id']==$playlist)
      {
          # This is a playlist
          $this-&gt;setIsPlaylist(TRUE);
  
          # Assign the Playlist Items to a variable.
          $video_search=$yt-&gt;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.';
  }
  </code></pre>
  
  <p>I have the code working for the single call but I can't figure out how to get the 404 error it returns if the playlist ID does not exist (results from YouTube below).</p>
  
  <p>YouTube method (<code>PlaylistItems</code>):</p>
  
  <pre><code>/**
   * 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' =&gt; 50, 'playlistId' =&gt; $playlist_id);
          $playlistsItems=$youtube_service-&gt;playlistItems-&gt;listPlaylistItems($part, $playlistsItems_optParams);
  
          $new_array=$this-&gt;rebuildArray($playlistsItems, TRUE);
  
          # Instantiate a new Utility object.
          $utility=new Utility();
          # Sort the playlist array by date.
          $items=$utility-&gt;sortByDate($new_array, 'publishedAt');
  
          return $items;
      }
      catch(Exception $e)
      {
          # Print Results (below)
          print_r($e); exit;
  
          if($e-&gt;getCode()==404)
          {
              $doc-&gt;setError('That playlist doesn\'t exist. Please choose another.');
          }
      }
  } #==== End -- PlaylistItems
  </code></pre>
  
  <p>Single call code:</p>
  
  <pre><code># Playlist ID from URL
  $playlist=$_GET['playlist'];
  
  # Assign the Playlist Items to a variable.
  $video_search=$yt-&gt;PlaylistItems($playlist);
  </code></pre>
  
  <p>Return from <code>PlaylistItems</code> method</p>
  
  <pre><code>Google_ServiceException Object
  (
      [errors:protected] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [domain] =&gt; youtube.playlistItem
                      [reason] =&gt; playlistNotFound
                      [message] =&gt; Not Found
                      [locationType] =&gt; parameter
                      [location] =&gt; playlistId
                  )
  
          )
  
      [message:protected] =&gt; Error calling GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&amp;maxResults=50&amp;playlistId=PLD2D6E64D9CD49E1&amp;key=AIzaSyDVGJhk7DfKKJAaciX14aqyw_YbPaT4fp4: (404) Not Found
      [string:Exception:private] =&gt; 
      [code:protected] =&gt; 404
      [file:protected] =&gt; /Volumes/Coding/WebDev/CWIS/cwis.org/application/modules/Social/Google/io/Google_REST.php
      [line:protected] =&gt; 66
      [trace:Exception:private] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [file] =&gt; /Volumes/Coding/WebDev/CWIS/cwis.org/application/modules/Social/Google/io/Google_REST.php
                      [line] =&gt; 36
                      [function] =&gt; decodeHttpResponse
                      [class] =&gt; Google_REST
                      [type] =&gt; ::
                      [args] =&gt; Array
                          (
                              [0] =&gt; Google_HttpRequest Object
                                  (
                                      [batchHeaders:Google_HttpRequest:private] =&gt; Array
                                          (
                                              [Content-Type] =&gt; application/http
                                              [Content-Transfer-Encoding] =&gt; binary
                                              [MIME-Version] =&gt; 1.0
                                              [Content-Length] =&gt; 
                                          )
  
                                      [url:protected] =&gt; https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&amp;maxResults=50&amp;playlistId=PLD2D6E64D9CD49E1&amp;key=AIzaSyDVGJhk7DfKKJAaciX14aqyw_YbPaT4fp4
                                      [requestMethod:protected] =&gt; GET
                                      [requestHeaders:protected] =&gt; Array
                                          (
                                          )
  
                                      [postBody:protected] =&gt; 
                                      [userAgent:protected] =&gt; google-api-php-client/0.6.0
                                      [responseHttpCode:protected] =&gt; 404
                                      [responseHeaders:protected] =&gt; Array
                                          (
                                              [content-type] =&gt; application/json; charset=UTF-8
                                              [date] =&gt; Thu, 18 Jul 2013 05:11:11 GMT
                                              [expires] =&gt; Thu, 18 Jul 2013 05:11:11 GMT
                                              [cache-control] =&gt; private, max-age=0
                                              [x-content-type-options] =&gt; nosniff
                                              [x-frame-options] =&gt; SAMEORIGIN
                                              [x-xss-protection] =&gt; 1; mode=block
                                              [server] =&gt; GSE
                                              [transfer-encoding] =&gt; chunked
                                          )
  
                                      [responseBody:protected] =&gt; {
   "error": {
    "errors": [
     {
      "domain": "youtube.playlistItem",
      "reason": "playlistNotFound",
      "message": "Not Found",
      "locationType": "parameter",
      "location": "playlistId"
     }
    ],
    "code": 404,
    "message": "Not Found"
   }
  }
  
                                      [accessKey] =&gt; 
                                  )
  
                          )
  
                  )
  
              [1] =&gt; Array
                  (
                      [file] =&gt; /Volumes/Coding/WebDev/CWIS/cwis.org/application/modules/Social/Google/service/Google_ServiceResource.php
                      [line] =&gt; 186
                      [function] =&gt; execute
                      [class] =&gt; Google_REST
                      [type] =&gt; ::
                      [args] =&gt; Array
                          (
                              [0] =&gt; Google_HttpRequest Object
                                  (
                                      [batchHeaders:Google_HttpRequest:private] =&gt; Array
                                          (
                                              [Content-Type] =&gt; application/http
                                              [Content-Transfer-Encoding] =&gt; binary
                                              [MIME-Version] =&gt; 1.0
                                              [Content-Length] =&gt; 
                                          )
  
                                      [url:protected] =&gt; https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&amp;maxResults=50&amp;playlistId=PLD2D6E64D9CD49E1&amp;key=AIzaSyDVGJhk7DfKKJAaciX14aqyw_YbPaT4fp4
                                      [requestMethod:protected] =&gt; GET
                                      [requestHeaders:protected] =&gt; Array
                                          (
                                          )
  
                                      [postBody:protected] =&gt; 
                                      [userAgent:protected] =&gt; google-api-php-client/0.6.0
                                      [responseHttpCode:protected] =&gt; 404
                                      [responseHeaders:protected] =&gt; Array
                                          (
                                              [content-type] =&gt; application/json; charset=UTF-8
                                              [date] =&gt; Thu, 18 Jul 2013 05:11:11 GMT
                                              [expires] =&gt; Thu, 18 Jul 2013 05:11:11 GMT
                                              [cache-control] =&gt; private, max-age=0
                                              [x-content-type-options] =&gt; nosniff
                                              [x-frame-options] =&gt; SAMEORIGIN
                                              [x-xss-protection] =&gt; 1; mode=block
                                              [server] =&gt; GSE
                                              [transfer-encoding] =&gt; chunked
                                          )
  
                                      [responseBody:protected] =&gt; {
   "error": {
    "errors": [
     {
      "domain": "youtube.playlistItem",
      "reason": "playlistNotFound",
      "message": "Not Found",
      "locationType": "parameter",
      "location": "playlistId"
     }
    ],
    "code": 404,
    "message": "Not Found"
   }
  }
  
                                      [accessKey] =&gt; 
                                  )
  
                          )
  
                  )
  
              [2] =&gt; Array
                  (
                      [file] =&gt; /Volumes/Coding/WebDev/CWIS/cwis.org/application/modules/Social/Google/contrib/Google_YoutubeService.php
                      [line] =&gt; 425
                      [function] =&gt; __call
                      [class] =&gt; Google_ServiceResource
                      [type] =&gt; -&gt;
                      [args] =&gt; Array
                          (
                              [0] =&gt; list
                              [1] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [part] =&gt; snippet
                                              [maxResults] =&gt; 50
                                              [playlistId] =&gt; PLD2D6E64D9CD49E1
                                          )
  
                                  )
  
                          )
  
                  )
  
              [3] =&gt; Array
                  (
                      [file] =&gt; /Volumes/Coding/WebDev/CWIS/cwis.org/application/modules/Media/YouTube.php
                      [line] =&gt; 412
                      [function] =&gt; listPlaylistItems
                      [class] =&gt; Google_PlaylistItemsServiceResource
                      [type] =&gt; -&gt;
                      [args] =&gt; Array
                          (
                              [0] =&gt; snippet
                              [1] =&gt; Array
                                  (
                                      [maxResults] =&gt; 50
                                      [playlistId] =&gt; PLD2D6E64D9CD49E1
                                  )
  
                          )
  
                  )
  
              [4] =&gt; Array
                  (
                      [file] =&gt; /Volumes/Coding/WebDev/CWIS/cwis.org/application/modules/Media/Video.php
                      [line] =&gt; 1130
                      [function] =&gt; PlaylistItems
                      [class] =&gt; YouTube
                      [type] =&gt; -&gt;
                      [args] =&gt; Array
                          (
                              [0] =&gt; PLD2D6E64D9CD49E1
                          )
  
                  )
  
              [5] =&gt; Array
                  (
                      [file] =&gt; /Volumes/Coding/WebDev/CWIS/cwis.org/public/media/video/index.php
                      [line] =&gt; 59
                      [function] =&gt; displayVideoFeed
                      [class] =&gt; Video
                      [type] =&gt; -&gt;
                      [args] =&gt; Array
                          (
                          )
  
                  )
  
          )
  
      [previous:Exception:private] =&gt; 
  )
  </code></pre>
  
  <p>As you an see everything is either <code>protected</code> or <code>private</code> so how am I supposed to know if there was an error or not?</p>