Youtube API V3 PHP按关键字搜索代码示例不起作用
我试着运行Google在开发人员代码示例页()上提供给您的示例代码,但对我不起作用。我确保从GitHub下载了该库的最新版本,将其安装到我的PHP5/Apache2服务器上,更改了包含路径,并且由于某些原因,其中一个require_once项无法正常工作。下面是我已经修改过的代码(仅使用一些echo语句进行了修改)。我输入了我的开发者密钥,但出于明显的原因,我在这篇文章中删除了它Youtube API V3 PHP按关键字搜索代码示例不起作用,php,youtube,youtube-api,Php,Youtube,Youtube Api,我试着运行Google在开发人员代码示例页()上提供给您的示例代码,但对我不起作用。我确保从GitHub下载了该库的最新版本,将其安装到我的PHP5/Apache2服务器上,更改了包含路径,并且由于某些原因,其中一个require_once项无法正常工作。下面是我已经修改过的代码(仅使用一些echo语句进行了修改)。我输入了我的开发者密钥,但出于明显的原因,我在这篇文章中删除了它 <?php $htmlBody = <<<END <form method="GET"
<?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/>";
第二个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的错误输出。如果发生错误,您将在生成的html中获得信息。您可以在中找到有关如何启用它的更多信息
- 确保
存在。导航到该文件夹并手动检查Google/Service/YouTube.php
- 在包含
之前先打印包含路径(我认为这可能不是问题,但谁知道…)YouTube.php
require_once 'include/Google/autoload.php';
在代码前面,删除所有其他要求,如:
//require_once 'include/Google/Client.php';
//require_once 'include/Google/Service/YouTube.php';
但是php版本应该大于5.3检查错误日志。文件可能没有显示错误报告,我得到以下错误<代码>致命错误:在第32行的/home/pi/Google-api-php-client/src/Google/Service/YouTube.php中找不到类“Google\u-Service”,听起来好像缺少一些依赖项。我不这么认为,但是您对库源代码做了任何更改吗?我添加了
require_once('Google/Service.php')
错误更改为致命错误:在第1332行的/home/pi/Google-api-php-client/src/Google/Service/YouTube.php中找不到类“Google\u-Service\u-Resource”
当我不断添加依赖项时,更多的内容会丢失。我是否缺少自动加载所需内容的内容,或者我必须继续手动搜索?我只是下载了库来重现问题,我也得到了它。要么我们俩都很傻,要么谷歌的代码中有一些奇怪的错误。
//require_once 'include/Google/Client.php';
//require_once 'include/Google/Service/YouTube.php';