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

Youtube API V3 PHP Search by Keyword code sample not working

我试过 运行ning Google 在他们的开发者代码示例页面 (https://developers.google.com/youtube/v3/code_samples/php#search_by_keyword) 上给你的示例代码,但对我不起作用。我确保从 GitHub 下载最新版本的库,将其安装到我的 PHP5/Apache2 服务器,更改了包含路径,但由于某种原因,require_once 项之一没有好好工作。这是我的代码(仅用一些 echo 语句修改)。我输入了我的开发人员密钥,但由于显而易见的原因,已将其从 post 中删除。

<?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 语句导致页面在此之后停止 运行ning 是怎么回事?有一次我在 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');

您可以尝试的一些事情:

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

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

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

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

您可以添加

require_once 'include/Google/autoload.php';

在代码前面,删除所有其他要求,如:

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

但是php版本应该大于5.3