Youtube API V3 PHP按关键字代码示例搜索不起作用

时间:2015-03-13 03:47:34

标签: php youtube youtube-api

我尝试运行Google在其开发人员代码示例页面(https://developers.google.com/youtube/v3/code_samples/php#search_by_keyword)上为您提供的示例代码对我不起作用。我确保从GitHub下载最新版本的库,将其安装到我的PHP5 / Apache2服务器,更改了包含路径,并且由于某种原因,其中一个require_once项目无法正常工作。这是我的代码(仅使用一些echo语句进行修改)。我输入了我的开发人员密钥,但由于显而易见的原因,已在此帖中删除了它。

<?php
$htmlBody = <<<END
<form method="GET">
  <div>
    Search Term: <input type="search" id="q" name="q" placeholder="Enter Search Term">
  </div>
  <div>
    Max Results: <input type="number" id="maxResults" name="maxResults" min="1" max="50" step="1" value="25">
  </div>
  <input type="submit" value="Search">
</form>
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']) {
    echo "echo 1";
    echo "<br/>";
  // Call set_include_path() as needed to point to your client library.
    set_include_path(get_include_path() . PATH_SEPARATOR . '/home/pi/google-api-php-client/src');
    require_once('Google/Client.php');
    echo "echo 2<br/>";
    require_once ('Google/Service/YouTube.php');
    echo "echo 3";
    echo "<br/>";

  /*
   * Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the
   * Google Developers Console <https://console.developers.google.com/>
   * Please ensure that you have enabled the YouTube Data API for your project.
   */
  $DEVELOPER_KEY = 'my-api-key-here';

  $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) {
      switch ($searchResult['id']['kind']) {
        case 'youtube#video':
          $videos .= sprintf('<li>%s (%s)</li>',
              $searchResult['snippet']['title'], $searchResult['id']['videoId']);
          break;
        case 'youtube#channel':
          $channels .= sprintf('<li>%s (%s)</li>',
              $searchResult['snippet']['title'], $searchResult['id']['channelId']);
          break;
        case 'youtube#playlist':
          $playlists .= sprintf('<li>%s (%s)</li>',
              $searchResult['snippet']['title'], $searchResult['id']['playlistId']);
          break;
      }
    }

    $htmlBody .= <<<END
    <h3>Videos</h3>
    <ul>$videos</ul>
    <h3>Channels</h3>
    <ul>$channels</ul>
    <h3>Playlists</h3>
    <ul>$playlists</ul>
END;
  } 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()));
  }
}
?>

<!doctype html>
<html>
  <head>
    <title>YouTube Search</title>
  </head>
  <body>
    <?=$htmlBody?>
  </body>
</html>

有趣的部分在这里

    echo "echo 1";
    echo "<br/>";
  // Call set_include_path() as needed to point to your client library.
    set_include_path(get_include_path() . PATH_SEPARATOR . '/home/pi/google-api-php-client/src');
    require_once('Google/Client.php');
    echo "echo 2<br/>";
    require_once ('Google/Service/YouTube.php');
    echo "echo 3";
    echo "<br/>";

当我运行页面时,输出就是这个

echo 1
echo 2

第二个require_once语句导致页面在此之后停止运行会发生什么?有一次,我在紧跟set_include_path()后面有一个echo语句,但这似乎工作正常,所以我删除了它。

我通过手动添加所有依赖项解决了这个问题。添加所有必需的依赖项之后,这就是我最终的结果。

require_once ('Google/Logger/Null.php');
require_once('Google/Client.php');
require_once ('Google/Config.php');
require_once ('Google/Model.php');
require_once ('Google/Collection.php');
require_once ('Google/Service.php');
require_once ('Google/Service/Resource.php');
require_once ('Google/Service/YouTube.php');

3 个答案:

答案 0 :(得分:2)

您可以添加

require_once 'include/Google/autoload.php';

在代码前面,并删除所有其他需要的内容:

//require_once 'include/Google/Client.php'; 
//require_once 'include/Google/Service/YouTube.php';

但是php版本应该大于5.3

答案 1 :(得分:0)

你可以尝试一些事情:

  • 打开PHP的错误输出。如果发生错误,您将在生成的html中获得信息。您可以在this thread

  • 中找到有关如何启用它的更多信息
  • 确保Google/Service/YouTube.php存在。导航到该文件夹​​并手动检查。

  • 在包含YouTube.php之前打印包含路径(我实际上并不认为这可能是问题,但谁知道......)

答案 2 :(得分:0)

看起来您没有正确设置PHP的包含路径。如果将此客户端库的src /文件添加到include路径中,它应该可以正常工作。