プレイリストをループして、プレイリストが Youtube に存在するかどうかを比較できることはわかっていますが、それは 2 つの呼び出しを行っています (以下の例)。プレイリスト ID は既にわかっているため、1 つの呼び出しを行うことができます。
プレイリストをループする YouTube への 2 つの呼び出し (私がやりたい方法ではありません):
# 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] =>
)
ご覧のとおり、すべてがどちらprotected
かprivate
であり、エラーがあったかどうかをどのように知ることができますか?