Skip to content

Instantly share code, notes, and snippets.

@tszym
Created July 7, 2014 09:15
Show Gist options
  • Save tszym/ec87fac9244270093060 to your computer and use it in GitHub Desktop.
Save tszym/ec87fac9244270093060 to your computer and use it in GitHub Desktop.

Revisions

  1. tszym created this gist Jul 7, 2014.
    105 changes: 105 additions & 0 deletions youtube-search.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    <?php

    // ...

    if ($client->getAccessToken()) {
    // Create analytics service object.
    $youtube = new Google_Service_YouTube($client);

    try {
    $keySearch = isset($_POST['key']) ? $_POST['key'] : '';
    $category = isset($_POST['videoCategoryId']) ? $_POST['videoCategoryId'] : '';

    $parameters = array(
    'q' => $keySearch,
    'order' => $_POST['order'],
    'videoDuration' => $_POST['videoDuration'],
    'maxResults' => 5,
    'type' => 'video', // To use videoEmbeddable
    'videoEmbeddable' => 'true' // displays only embeddable videos
    );

    if ($category !== '')
    $parameters['videoCategoryId'] = $category;

    // Call the channels.list method to retrieve information about the
    // currently authenticated user's channel.
    $searchResponse = $youtube->search->listSearch('snippet', $parameters);

    $htmlBody = '';

    $htmlBody .= "<h3>Search results for key $keySearch";
    $htmlBody .= sprintf('(%s results)</h3><ul>', $searchResponse['pageInfo']['totalResults']);

    foreach ($searchResponse['items'] as $searchResultItem) {

    $htmlBody .= sprintf('<li><a href="https://www.youtube.com/watch?v=%s">%s (%s)</a></li>',
    $searchResultItem['id']['videoId'],
    $searchResultItem['snippet']['title'],
    $searchResultItem['id']['videoId']);
    $htmlBody .= sprintf('<img src="%s" />', $searchResultItem['snippet']['thumbnails']['default']['url']);
    $htmlBody .= sprintf('<br><p>%s</p>', $searchResultItem['snippet']['description']);
    }
    $htmlBody .= '</ul>';

    $categoryChoices = '';
    // Get the categories allowed in the US, and displays in french
    $categories = $youtube->videoCategories->listVideoCategories('snippet', array(
    'hl' => 'fr_FR',
    'regionCode' => 'US'
    ));

    foreach ($categories as $category) {
    $categoryChoices .= sprintf('<option value="%s">%s</option>',
    $category['snippet']['channelId'],
    $category['snippet']['title']);
    }

    } catch (Google_ServiceException $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
    htmlspecialchars($e->getMessage()));
    } catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
    htmlspecialchars($e->getMessage()));
    }
    $_SESSION['token'] = $client->getAccessToken();

    } else {
    // ...
    }
    ?>

    <!doctype html>
    <html>
    <head>
    <title>Search Youtube</title>
    <meta charset="UTF-8">
    </head>
    <body>
    <form method="post">
    <label for="key">Search</label>
    <input type="text" name="key" />
    <label for="order">Search</label>
    <select name="order">
    <option value="relevance" selected>Relevance</option>
    <option value="rating">rating</option>
    <option value="title">title</option>
    <option value="viewCount">viewCount</option>
    <option value="date">date (recent)</option>
    </select>
    <label for="videoDuration">Duration</label>
    <select name="videoDuration">
    <option value="any" selected>any</option>
    <option value="short">short</option>
    <option value="long">long</option>
    <option value="medium">medium</option>
    </select>
    <label for="videoCategoryId">Category</label>
    <select name="videoCategoryId">
    <?php echo $categoryChoices; ?>
    </select>
    <input type="submit" value="Search" />
    </form>
    <?php echo $htmlBody; ?>
    </body>
    </html>