Search Term:
Max Results:
END; // This code will execute if the user entered a search query in the form // and submitted the form. Otherwise, the page displays the form above. if ($_GET['q'] && $_GET['maxResults']) { // Call set_include_path() as needed to point to your client library. require_once 'Google/autoload.php'; //require_once 'Google/Service/YouTube.php'; /* * 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 = 'AIzaSyDOHJkmchgk4rE7_XuiEGalQnFnSf15hfs'; $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( 'q' => $_GET['q'], 'maxResults' => $_GET['maxResults'], )); $videos = ''; $channels = ''; $playlists = ''; // Add each result to the appropriate list, and then display the lists of // matching videos, channels, and playlists. foreach ($searchResponse['items'] as $searchResult) { var_dump($searchResult); switch ($searchResult['id']['kind']) { case 'youtube#video': $videos .= sprintf('
  • %s (%s)
  • ', $searchResult['id']['videoId'],$searchResult['snippet']['title'], $searchResult['id']['videoId']); break; case 'youtube#channel': $channels .= sprintf('
  • %s (%s)
  • ', $searchResult['snippet']['title'], $searchResult['id']['channelId']); break; case 'youtube#playlist': $playlists .= sprintf('
  • %s (%s)
  • ', $searchResult['snippet']['title'], $searchResult['id']['playlistId']); break; } } $htmlBody .= <<Videos

    Channels

    Playlists

    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 Search