The YouTube Data API allows access to YouTube content; you can use this API to fetch videos, playlists, channels, post comments, and upload and manage videos. Users are allowed
to perform unauthenticated requests for the retrieval of feeds on popular videos, post comments, and so on.
Some of the most frequently used YouTube API methods are listed as follows:
◆ getVideoFeed(): Retrieve videos from a video query
◆ getTopRatedVideoFeed(): Retrieve top-rated videos for the specific video query
◆ getUserUploads(): Retrieve the user's uploaded videos
◆ getUserFavorites(): Retrieve the user's favorite videos
◆ getVideoResponseFeed(): Get video responses for a specific video
◆ getVideoCommentFeed(): Get comments for a specific video
◆ getPlaylistListFeed(): Get a user's playlists
◆ getSubscriptionFeed(): Get a user's subscriptions
◆ insertEntry(): Upload a video to YouTube
In this example, we will be retrieving videos for a specific keyword and then render them in the web page.
Perform the following steps for listing YouTube videos for a keyword:
1. Create a function that will get the YouTube videos for the Zend Framework keyword.
2. Establish the connection in a similar way to the previous connection made for Google Photos. This needs to be placed in a new method, getYoutubeVideos(), in the MediaManagerController file,src/Users/Controller/ MediaManagerController.php:
$adapter = new \Zend\Http\Client\Adapter\Curl();
$adapter->setOptions(array( 'curloptions' => array(
CURLOPT_SSL_VERIFYPEER => false,
)
));
$httpClient = new \ZendGData\HttpClient();
$httpClient->setAdapter($adapter);
$client = \ZendGData\ClientLogin::getHttpClient( self::GOOGLE_USER_ID,
self::GOOGLE_PASSWORD,
\ZendGData\YouTube::AUTH_SERVICE_NAME,
$httpClient);
3. Initialize the YouTube client and execute a video query for the keyword
Zend Framework:
$yt = new \ZendGData\YouTube($client);
$yt->setMajorProtocolVersion(2);
$query = $yt->newVideoQuery();
$query->setOrderBy('relevance');
$query->setSafeSearch('none');
$query->setVideoQuery('Zend Framework');
4. Parse the query results and store it in an array:
$videoFeed = $yt->getVideoFeed($query->getQueryUrl(2));
$yVideos = array();
foreach ($videoFeed as $videoEntry) {
$yVideo = array();
$yVideo['videoTitle'] = $videoEntry->getVideoTitle();
$yVideo['videoDescription'] =
$videoEntry->getVideoDescription();
$yVideo['watchPage'] = $videoEntry->getVideoWatchPageUrl();
$yVideo['duration'] = $videoEntry->getVideoDuration();
$videoThumbnails = $videoEntry->getVideoThumbnails();
$yVideo['thumbnailUrl'] = $videoThumbnails[0]['url'];
$yVideos[] = $yVideo;
}
return $yVideos;
5. The resulting content is rendered in the view and a video listing as shown in the following screenshot:
What just happened?
We have utilized the ZendGData API's YouTube APIs to retrieve a simple list of videos from YouTube for a specific keyword.
Q1. Which command is used in Composer to install a newly configured dependency?
1. php composer.phar setup
2. php composer.phar self-update
3. php composer.phar show
4. php composer.phar update
1. uploadPhoto()
2. insertPhoto()
3. uploadNewPhoto()
4. insertPhotoEntry()
Summary
In this chapter, we have learned various techniques to manage media; initially we started with implementing our own photo gallery and later on we moved on to using Google GData APIs to retrieve and store media on the Web.
In our next chapter, we will be working on implementing a simple search interface.
Không có nhận xét nào:
Đăng nhận xét