Created
July 7, 2014 09:15
-
-
Save tszym/ec87fac9244270093060 to your computer and use it in GitHub Desktop.
Youtube Data API v3 - Search videos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ... | |
if ($client->getAccessToken()) { | |
// Create analytics service object. | |
$youtube = new Google_Service_YouTube($client); | |
try { | |
$keySearch = isset($_POST['key']) ? $_POST['key'] : ''; | |
$category = isset($_POST['videoCategoryId']) ? $_POST['videoCategoryId'] : ''; | |
$parameters = array( | |
'q' => $keySearch, | |
'order' => $_POST['order'], | |
'videoDuration' => $_POST['videoDuration'], | |
'maxResults' => 5, | |
'type' => 'video', // To use videoEmbeddable | |
'videoEmbeddable' => 'true' // displays only embeddable videos | |
); | |
if ($category !== '') | |
$parameters['videoCategoryId'] = $category; | |
// Call the channels.list method to retrieve information about the | |
// currently authenticated user's channel. | |
$searchResponse = $youtube->search->listSearch('snippet', $parameters); | |
$htmlBody = ''; | |
$htmlBody .= "<h3>Search results for key $keySearch"; | |
$htmlBody .= sprintf('(%s results)</h3><ul>', $searchResponse['pageInfo']['totalResults']); | |
foreach ($searchResponse['items'] as $searchResultItem) { | |
$htmlBody .= sprintf('<li><a href="https://www.youtube.com/watch?v=%s">%s (%s)</a></li>', | |
$searchResultItem['id']['videoId'], | |
$searchResultItem['snippet']['title'], | |
$searchResultItem['id']['videoId']); | |
$htmlBody .= sprintf('<img src="%s" />', $searchResultItem['snippet']['thumbnails']['default']['url']); | |
$htmlBody .= sprintf('<br><p>%s</p>', $searchResultItem['snippet']['description']); | |
} | |
$htmlBody .= '</ul>'; | |
$categoryChoices = ''; | |
// Get the categories allowed in the US, and displays in french | |
$categories = $youtube->videoCategories->listVideoCategories('snippet', array( | |
'hl' => 'fr_FR', | |
'regionCode' => 'US' | |
)); | |
foreach ($categories as $category) { | |
$categoryChoices .= sprintf('<option value="%s">%s</option>', | |
$category['snippet']['channelId'], | |
$category['snippet']['title']); | |
} | |
} catch (Google_ServiceException $e) { | |
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', | |
htmlspecialchars($e->getMessage())); | |
} catch (Google_Exception $e) { | |
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', | |
htmlspecialchars($e->getMessage())); | |
} | |
$_SESSION['token'] = $client->getAccessToken(); | |
} else { | |
// ... | |
} | |
?> | |
<!doctype html> | |
<html> | |
<head> | |
<title>Search Youtube</title> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<form method="post"> | |
<label for="key">Search</label> | |
<input type="text" name="key" /> | |
<label for="order">Search</label> | |
<select name="order"> | |
<option value="relevance" selected>Relevance</option> | |
<option value="rating">rating</option> | |
<option value="title">title</option> | |
<option value="viewCount">viewCount</option> | |
<option value="date">date (recent)</option> | |
</select> | |
<label for="videoDuration">Duration</label> | |
<select name="videoDuration"> | |
<option value="any" selected>any</option> | |
<option value="short">short</option> | |
<option value="long">long</option> | |
<option value="medium">medium</option> | |
</select> | |
<label for="videoCategoryId">Category</label> | |
<select name="videoCategoryId"> | |
<?php echo $categoryChoices; ?> | |
</select> | |
<input type="submit" value="Search" /> | |
</form> | |
<?php echo $htmlBody; ?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment