【发布时间】:2015-04-21 18:16:37
【问题描述】:
我尝试运行 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');
【问题讨论】:
-
检查错误日志。文件可能不存在
标签: php youtube youtube-api