|
| 1 | +<?php |
| 2 | + |
| 3 | +$htmlBody = <<<END |
| 4 | +<form method="GET"> |
| 5 | + <div> |
| 6 | + Search Term: <input type="search" id="q" name="q" placeholder="Enter Search Term"> |
| 7 | + </div> |
| 8 | + <div> |
| 9 | + Max Results: <input type="number" id="maxResults" name="maxResults" min="1" max="50" step="1" value="25"> |
| 10 | + </div> |
| 11 | + <input type="submit" value="Search"> |
| 12 | +</form> |
| 13 | +END; |
| 14 | + |
| 15 | +// This code will execute if the user entered a search query in the form |
| 16 | +// and submitted the form. Otherwise, the page displays the form above. |
| 17 | +if ($_GET['q'] && $_GET['maxResults']) { |
| 18 | + // Call set_include_path() as needed to point to your client library. |
| 19 | +require_once 'Google/Client.php'; |
| 20 | +require_once 'Google/Service/YouTube.php'; |
| 21 | + |
| 22 | + /* |
| 23 | + * Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the |
| 24 | + * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}> |
| 25 | + * Please ensure that you have enabled the YouTube Data API for your project. |
| 26 | + */ |
| 27 | + $DEVELOPER_KEY = 'REPLACE_ME'; |
| 28 | + |
| 29 | + $client = new Google_Client(); |
| 30 | + $client->setDeveloperKey($DEVELOPER_KEY); |
| 31 | + |
| 32 | + // Define an object that will be used to make all API requests. |
| 33 | + $youtube = new Google_Service_YouTube($client); |
| 34 | + |
| 35 | + try { |
| 36 | + // Call the search.list method to retrieve results matching the specified |
| 37 | + // query term. |
| 38 | + $searchResponse = $youtube->search->listSearch('id,snippet', array( |
| 39 | + 'q' => $_GET['q'], |
| 40 | + 'maxResults' => $_GET['maxResults'], |
| 41 | + )); |
| 42 | + |
| 43 | + $videos = ''; |
| 44 | + $channels = ''; |
| 45 | + $playlists = ''; |
| 46 | + |
| 47 | + // Add each result to the appropriate list, and then display the lists of |
| 48 | + // matching videos, channels, and playlists. |
| 49 | + foreach ($searchResponse['items'] as $searchResult) { |
| 50 | + switch ($searchResult['id']['kind']) { |
| 51 | + case 'youtube#video': |
| 52 | + $videos .= sprintf('<li>%s (%s)</li>', |
| 53 | + $searchResult['snippet']['title'], $searchResult['id']['videoId']); |
| 54 | + break; |
| 55 | + case 'youtube#channel': |
| 56 | + $channels .= sprintf('<li>%s (%s)</li>', |
| 57 | + $searchResult['snippet']['title'], $searchResult['id']['channelId']); |
| 58 | + break; |
| 59 | + case 'youtube#playlist': |
| 60 | + $playlists .= sprintf('<li>%s (%s)</li>', |
| 61 | + $searchResult['snippet']['title'], $searchResult['id']['playlistId']); |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + $htmlBody .= <<<END |
| 67 | + <h3>Videos</h3> |
| 68 | + <ul>$videos</ul> |
| 69 | + <h3>Channels</h3> |
| 70 | + <ul>$channels</ul> |
| 71 | + <h3>Playlists</h3> |
| 72 | + <ul>$playlists</ul> |
| 73 | +END; |
| 74 | + } catch (Google_ServiceException $e) { |
| 75 | + $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', |
| 76 | + htmlspecialchars($e->getMessage())); |
| 77 | + } catch (Google_Exception $e) { |
| 78 | + $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', |
| 79 | + htmlspecialchars($e->getMessage())); |
| 80 | + } |
| 81 | +} |
| 82 | +?> |
| 83 | + |
| 84 | +<!doctype html> |
| 85 | +<html> |
| 86 | + <head> |
| 87 | + <title>YouTube Search</title> |
| 88 | + </head> |
| 89 | + <body> |
| 90 | + <?=$htmlBody?> |
| 91 | + </body> |
| 92 | +</html> |
0 commit comments