php - 如何从搜索中获取第一个 YouTube 视频的 URL?

标签 php video youtube

关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

8年前关闭。




Improve this question




我有一个包含电影片的网站,我想展示电影的预告片。
为了做到这一点,是否可以从 YouTube 搜索中获取热门 YouTube 视频的 URL?

由于在大多数情况下键入“[电影名称]预告片”效果很好,而且预告片通常是 HitTest 门的视频,因此很容易。

在另一个主题上,如果选择的视频不正确,让用户修改视频的最佳方法是什么?使用投票系统,因此至少需要 4-5 票才能更改视频。

最佳答案

这是我使用谷歌的 youtube api 在我的旧网站上使用的 youtube 搜索

<?php
// Call set_include_path() as needed to point to your client library.
  require_once ('video/Google_Client.php');
  require_once ('video/Google_YouTubeService.php');

  /* Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the
  Google APIs Console <http://code.google.com/apis/console#access>
  Please ensure that you have enabled the YouTube Data API for your project. */
  $DEVELOPER_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

  $client = new Google_Client();
  $client->setDeveloperKey($DEVELOPER_KEY);

  $youtube = new Google_YoutubeService($client);

  try {
    $searchResponse = $youtube->search->listSearch('id,snippet', array(
      'q' => $_POST['query'],
      'maxResults' => '48',
    ));

    $videos = '';
    $channels = '';

    foreach ($searchResponse['items'] as $searchResult) {
      switch ($searchResult['id']['kind']) {
        case 'youtube#video':
          $videos .=  $searchResult;


          break;

       }
    }

   } 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()));
  }


echo print_r($videos);
?>

我使用了一个表单来发布搜索查询(注意 $_POST['search'] 变量,这将是要搜索的字符串,在你的情况下是电影名称)

关于php - 如何从搜索中获取第一个 YouTube 视频的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18679004/

相关文章:

javascript - 嵌入表单集合

php - 使用 PHP 将二进制文件转换为文件

youtube - Youtube iframe功能可在youtube视频结尾打开网站

css - 将 CSS 添加到 <a> 标记不起作用

php - Laravel 中如何创建全局变量

php - 如何计算 JavaScript 中的行数?

android - Android 中的视频编辑 API

video - 来自 VLC 的 HTTP 流式传输在 15 秒后卡住

video - 使用ffmpeg旋转视频添加黑条

video - 如何编辑youtube-dl命令以在视频文件名中包含额外的关键字