|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This sample lists videos that are associated with a particular keyword and are in the radius of |
| 5 | + * particular geographic coordinates by: |
| 6 | + * |
| 7 | + * 1. Searching videos with "youtube.search.list" method and setting "type", "q", "location" and |
| 8 | + * "locationRadius" parameters. |
| 9 | + * 2. Retrieving location details for each video with "youtube.videos.list" method and setting |
| 10 | + * "id" parameter to comma separated list of video IDs in search result. |
| 11 | + * |
| 12 | + * @author Ibrahim Ulukaya |
| 13 | + */ |
| 14 | + |
| 15 | +$htmlBody = <<<END |
| 16 | +<form method="GET"> |
| 17 | + <div> |
| 18 | + Search Term: <input type="search" id="q" name="q" placeholder="Enter Search Term"> |
| 19 | + </div> |
| 20 | + <div> |
| 21 | + Location: <input type="text" id="location" name="location" placeholder="37.42307,-122.08427"> |
| 22 | + </div> |
| 23 | + <div> |
| 24 | + Location Radius: <input type="text" id="locationRadius" name="locationRadius" placeholder="5km"> |
| 25 | + </div> |
| 26 | + <div> |
| 27 | + Max Results: <input type="number" id="maxResults" name="maxResults" min="1" max="50" step="1" value="25"> |
| 28 | + </div> |
| 29 | + <input type="submit" value="Search"> |
| 30 | +</form> |
| 31 | +END; |
| 32 | + |
| 33 | +// This code executes if the user enters a search query in the form |
| 34 | +// and submits the form. Otherwise, the page displays the form above. |
| 35 | +if ($_GET['q'] && $_GET['maxResults']) { |
| 36 | + // Call set_include_path() as needed to point to your client library. |
| 37 | + require_once 'Google/Client.php'; |
| 38 | + require_once 'Google/Service/YouTube.php'; |
| 39 | + |
| 40 | + /* |
| 41 | + * Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the |
| 42 | + * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}> |
| 43 | + * Please ensure that you have enabled the YouTube Data API for your project. |
| 44 | + */ |
| 45 | + $DEVELOPER_KEY = 'REPLACE_ME'; |
| 46 | + |
| 47 | + $client = new Google_Client(); |
| 48 | + $client->setDeveloperKey($DEVELOPER_KEY); |
| 49 | + |
| 50 | + // Define an object that will be used to make all API requests. |
| 51 | + $youtube = new Google_Service_YouTube($client); |
| 52 | + |
| 53 | + try { |
| 54 | + // Call the search.list method to retrieve results matching the specified |
| 55 | + // query term. |
| 56 | + $searchResponse = $youtube->search->listSearch('id,snippet', array( |
| 57 | + 'type' => 'video', |
| 58 | + 'q' => $_GET['q'], |
| 59 | + 'location' => $_GET['location'], |
| 60 | + 'locationRadius' => $_GET['locationRadius'], |
| 61 | + 'maxResults' => $_GET['maxResults'], |
| 62 | + )); |
| 63 | + |
| 64 | + $videoResults = array(); |
| 65 | + # Merge video ids |
| 66 | + foreach ($searchResponse['items'] as $searchResult) { |
| 67 | + array_push($videoResults, $searchResult['id']['videoId']); |
| 68 | + } |
| 69 | + $videoIds = join(',', $videoResults); |
| 70 | + |
| 71 | + # Call the videos.list method to retrieve location details for each video. |
| 72 | + $videosResponse = $youtube->videos->listVideos('snippet, recordingDetails', array( |
| 73 | + 'id' => $videoIds, |
| 74 | + )); |
| 75 | + |
| 76 | + $videos = ''; |
| 77 | + |
| 78 | + // Display the list of matching videos. |
| 79 | + foreach ($videosResponse['items'] as $videoResult) { |
| 80 | + $videos .= sprintf('<li>%s (%s,%s)</li>', |
| 81 | + $videoResult['snippet']['title'], |
| 82 | + $videoResult['recordingDetails']['location']['latitude'], |
| 83 | + $videoResult['recordingDetails']['location']['longitude']); |
| 84 | + } |
| 85 | + |
| 86 | + $htmlBody .= <<<END |
| 87 | + <h3>Videos</h3> |
| 88 | + <ul>$videos</ul> |
| 89 | +END; |
| 90 | + } catch (Google_ServiceException $e) { |
| 91 | + $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', |
| 92 | + htmlspecialchars($e->getMessage())); |
| 93 | + } catch (Google_Exception $e) { |
| 94 | + $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', |
| 95 | + htmlspecialchars($e->getMessage())); |
| 96 | + } |
| 97 | +} |
| 98 | +?> |
| 99 | + |
| 100 | +<!doctype html> |
| 101 | +<html> |
| 102 | +<head> |
| 103 | +<title>YouTube Geolocation Search</title> |
| 104 | +</head> |
| 105 | +<body> |
| 106 | + <?=$htmlBody?> |
| 107 | +</body> |
| 108 | +</html> |
0 commit comments