Search Term:
Location:
Location Radius:
Max Results:
END; // This code executes if the user enters a search query in the form // and submits the form. Otherwise, the page displays the form above. if (isset($_GET['q']) && isset($_GET['maxResults'])) { /* * Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}> * Please ensure that you have enabled the YouTube Data API for your project. */ $DEVELOPER_KEY = 'REPLACE_ME'; $client = new Google_Client(); $client->setDeveloperKey($DEVELOPER_KEY); // Define an object that will be used to make all API requests. $youtube = new Google_Service_YouTube($client); try { // Call the search.list method to retrieve results matching the specified // query term. $searchResponse = $youtube->search->listSearch('id,snippet', array( 'type' => 'video', 'q' => $_GET['q'], 'location' => $_GET['location'], 'locationRadius' => $_GET['locationRadius'], 'maxResults' => $_GET['maxResults'], )); $videoResults = array(); # Merge video ids foreach ($searchResponse['items'] as $searchResult) { array_push($videoResults, $searchResult['id']['videoId']); } $videoIds = join(',', $videoResults); # Call the videos.list method to retrieve location details for each video. $videosResponse = $youtube->videos->listVideos('snippet, recordingDetails', array( 'id' => $videoIds, )); $videos = ''; // Display the list of matching videos. foreach ($videosResponse['items'] as $videoResult) { $videos .= sprintf('
  • %s (%s,%s)
  • ', $videoResult['snippet']['title'], $videoResult['recordingDetails']['location']['latitude'], $videoResult['recordingDetails']['location']['longitude']); } $htmlBody .= <<Videos END; } catch (Google_Service_Exception $e) { $htmlBody .= sprintf('

    A service error occurred: %s

    ', htmlspecialchars($e->getMessage())); } catch (Google_Exception $e) { $htmlBody .= sprintf('

    An client error occurred: %s

    ', htmlspecialchars($e->getMessage())); } } ?> YouTube Geolocation Search