-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_video_tutorials.php
53 lines (47 loc) · 1.53 KB
/
list_video_tutorials.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
* Get json array videos.
*
* @param String $occupation
* The occupation requested.
* @param int $count
* How many videos are going to be displayed.
*
* @return string
* JSON array of videos.
*/
function list_video_tutorials($occupation, $count = 3) {
if (empty($occupation)) {
return '';
}
include_once __DIR__ . '/google-api-php-client/vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
// Warn if the API key isn't set.
$apiKey = 'AIzaSyDfJZY4eDfxVRgvNRabzqKaCKLZr35aKmM';
$client->setDeveloperKey($apiKey);
$youtube_service = new Google_Service_YouTube($client);
$searchResponse = $youtube_service->search->listSearch('id,snippet', array(
'q' => "What is " . $occupation,
'maxResults' => $count,
));
$videos = array();
// Add each result to the appropriate list, and then display the lists of
// matching videos, channels, and playlists.
foreach ($searchResponse['items'] as $searchResult) {
switch ($searchResult['id']['kind']) {
case 'youtube#video':
$videos[] = array(
'videoId' => $searchResult['id']['videoId'],
'title' => $searchResult['snippet']['title'],
'description' => $searchResult['snippet']['description'],
);
break;
}
}
return $videos;
}
$occupation = !empty($_GET['occupation']) ? $_GET['occupation'] : '';
$count = !empty($_GET['count']) ? $_GET['count'] : 3;
print json_encode(list_video_tutorials($occupation, $count));
exit();