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



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

<?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');

您可以添加

require_once 'include/Google/autoload.php';

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

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

但是 php 版本应该大于 5.3

您可以尝试以下方法:

  • 打开 PHP 的错误输出。如果发生错误,则会在生成的 html 中获取信息。您可以在此线程中找到有关如何启用它的更多信息。

  • 确保Google/Service/YouTube.php存在。导航到该文件夹并手动检查。

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

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

相关内容

  • 没有找到相关文章

最新更新