【问题标题】:How to get the URL of the first YouTube video from a search? [closed]如何从搜索中获取第一个 YouTube 视频的 URL? [关闭]
【发布时间】:2013-09-07 23:43:06
【问题描述】:

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

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

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

【问题讨论】:

  • 我认为这可以通过使用googles youtube data api 来简化很多
  • 谢谢!我刚刚注册了一个 Google API 帐户,你知道我如何使用 YouTube API 来实现吗?

标签: php video youtube


【解决方案1】:

这是我使用谷歌的 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'] 变量,这将是要搜索的字符串,在你的情况下是电影名称)

【讨论】:

  • 谢谢!我的代码类似,我只是将最大结果编辑为 1 以获得第一个视频。效果很好!
猜你喜欢
  • 2019-01-11
  • 2018-01-21
  • 2020-01-05
  • 2012-08-04
  • 1970-01-01
  • 2012-05-22
  • 2011-03-28
  • 2013-01-23
相关资源
最近更新 更多