Mr.6 簡介

Mr.6 本名劉威麟,作家,亦被公認為最早開始推動台灣網路創業風潮的推手之一,美國史丹佛大學(Stanford University)電機、管理雙碩士,從小移民加拿大,而後旅居矽谷多年,2002年~2009年間著有12本書,其中《搶先佈局十年後》於十年前預中多項科技應用之實現,並於2006年6月6日起,於個人部落格 Mr.6 - 趨勢.創業.網路.生活 發表了超過4300篇網站分析、科技趨勢、職場觀點文章,與Mr.6聯絡或邀請演講請歡迎來信 send.to.mr6@gmail.com

歡迎加入粉絲團

關於自動販賣機的熱門YouTube影片

= 5.1.4
*
* This sample is run from within a web browser. These files are required:
* index.php – the main logic, which interfaces with the YouTube API
* interface.html – the HTML to represent the web UI
* web_browser.css – the CSS to define the interface style
* web_browser.js – the JavaScript used to provide the video list AJAX interface
*
* NOTE: If using in production, some additional precautions with regards
* to filtering the input data should be used. This code is designed only
* for demonstration purposes.
*/
require_once ‘Zend/Loader.php’;
Zend_Loader::loadClass(‘Zend_Gdata_YouTube’);

/**
* Finds the URL for the flash representation of the specified video
*
* @param Zend_Gdata_YouTube_VideoEntry $entry The video entry
* @return (string|null) The URL or null, if the URL is not found
*/
function findFlashUrl($entry)
{
foreach ($entry->mediaGroup->content as $content) {
if ($content->type === ‘application/x-shockwave-flash’) {
return $content->url;
}
}
return null;
}

/**
* Returns a feed of top rated videos for the specified user
*
* @param string $user The username
* @return Zend_Gdata_YouTube_VideoFeed The feed of rated videos
*/
function getTopRatedVideosByUser($user)
{
$userVideosUrl = ‘http://gdata.youtube.com/feeds/users/’ .
$user . ‘/uploads’;
$yt = new Zend_Gdata_YouTube();
$ytQuery = $yt->newVideoQuery($userVideosUrl);
// order by the rating of the videos
$ytQuery->setOrderBy(‘rating’);
// retrieve a maximum of 5 videos
$ytQuery->setMaxResults(5);
// retrieve only embeddable videos
$ytQuery->setFormat(5);
return $yt->getVideoFeed($ytQuery);
}

/**
* Returns a feed of videos related to the specified video
*
* @param string $videoId The video
* @return Zend_Gdata_YouTube_VideoFeed The feed of related videos
*/
function getRelatedVideos($videoId)
{
$yt = new Zend_Gdata_YouTube();
$ytQuery = $yt->newVideoQuery();
// show videos related to the specified video
$ytQuery->setFeedType(‘related’, $videoId);
// order videos by rating
$ytQuery->setOrderBy(‘rating’);
// retrieve a maximum of 5 videos
$ytQuery->setMaxResults(5);
// retrieve only embeddable videos
$ytQuery->setFormat(5);
return $yt->getVideoFeed($ytQuery);
}

/**
* Echo img tags for the first thumbnail representing each video in the
* specified video feed. Upon clicking the thumbnails, the video should
* be presented.
*
* @param Zend_Gdata_YouTube_VideoFeed $feed The video feed
*/
function echoThumbnails($feed)
{
foreach ($feed as $entry) {
$videoId = $entry->getVideoId();
echo ‘${videoTitle}

觀賞次數:${viewCount}

END;
echo ‘
‘;
}

/**
* Echo video metadata
*
* @param Zend_Gdata_YouTube_VideoEntry $entry The video entry
*/
function echoVideoMetadata($entry)
{
$title = $entry->mediaGroup->title;
$description = $entry->mediaGroup->description;
$authorUsername = $entry->author[0]->name;
$authorUrl = ‘http://www.youtube.com/profile?user=’ . $authorUsername;
$tags = $entry->mediaGroup->keywords;
$duration = $entry->mediaGroup->duration->seconds;
$watchPage = $entry->mediaGroup->player[0]->url;
$viewCount = $entry->statistics->viewCount;
$rating = $entry->rating->average;
$numRaters = $entry->rating->numRaters;
$flashUrl = findFlashUrl($entry);
print <<Title: ${title}
Description: ${description}
Author: ${authorUsername}
Tags: ${tags}
Duration: ${duration} seconds
View count: ${viewCount}
Rating: ${rating} (${numRaters} ratings)
Flash: ${flashUrl}
Watch page: ${watchPage}
END;
}

/**
* Echo the list of videos in the specified feed.
*
* @param Zend_Gdata_YouTube_VideoFeed $feed The video feed
*/
function echoVideoList($feed)
{
echo ‘

‘;
echo ‘

‘;
foreach ($feed as $entry) {
$videoId = $entry->getVideoId();

$thumbnailUrl = $entry->mediaGroup->thumbnail[0]->url;
$videoTitle = $entry->mediaGroup->title;
$videoDescription = $entry->mediaGroup->description;
print <<

‘;
echo ‘

‘;

}
echo ‘

END;
echoVideoPlayer( $videoId );
echo ‘

‘;
}

/*
* The main controller logic of the YouTube video browser demonstration app.
*/
/* $queryType = isset($_POST[‘queryType’]) ? $_POST[‘queryType’] : null; */
$queryType = ‘all’;

if ($queryType === null) {
/* display the entire interface */
include ‘interface.html’;
} else if ($queryType == ‘show_video’) {
/* display an individual video */
if (array_key_exists(‘videoId’, $_POST)) {
$videoId = $_POST[‘videoId’];
echoVideoPlayer($videoId);
} else if (array_key_exists(‘videoId’, $_GET)) {
$videoId = $_GET[‘videoId’];
echoVideoPlayer($videoId);
} else {
echo ‘No videoId found.’;
exit;
}
} else {
/* display a list of videos */
/* $searchTerm = $_POST[‘searchTerm’];
$startIndex = $_POST[‘startIndex’];
$maxResults = $_POST[‘maxResults’]; */

$searchTerm = ‘vending machine’;
$startIndex = 1;
$maxResults = 10;

$yt = new Zend_Gdata_YouTube();
$query = $yt->newVideoQuery();
$query->setQuery($searchTerm);
$query->setStartIndex($startIndex);
$query->setMaxResults($maxResults);
$query->setOrderBy(‘viewCount’);
/* $query->setTime(‘this_week’); */

/* check for one of the standard feeds, or list from ‘all’ videos */
switch ($queryType) {
case ‘most_viewed’:
$query->setFeedType(‘most viewed’);
$query->setTime(‘all_time’);
$feed = $yt->getVideoFeed($query);
break;
case ‘most_recent’:
$query->setFeedType(‘most recent’);
$query->setTime(‘this_week’);
$feed = $yt->getVideoFeed($query);
break;
case ‘recently_featured’:
$query->setFeedType(‘recently featured’);
$query->setTime(‘this_week’);
$feed = $yt->getVideoFeed($query);
break;
case ‘top_rated’:
$query->setFeedType(‘top rated’);
$query->setTime(‘this_week’);
$feed = $yt->getVideoFeed($query);
break;
case ‘all’:
$feed = $yt->getVideoFeed($query);
break;
default:
echo ‘ERROR – unknown queryType – “‘ . $queryType . ‘”‘;
break;
}
echoVideoList($feed);
/* echoVideoPlayer($feed); */
}
?>