我是一名学习者级别的开发人员......
几周前谷歌关闭了API v2,而我的开发项目已被破坏。我对这个API密钥系统感到困惑。因此,我尝试重建我的脚本,并使用google中的示例代码段替换搜索页。https://developers.google.com/youtube/v3/code_samples/php#search_by_keyword
我用我生成的api密钥替换了---$DEVELOPER_KEY = 'REPLACE_ME';
的 replace_me ...这是我的设置。 http://i.imgur.com/0L8IUip.jpg但仍无法正常工作..
我将精确的代码片段发送到名为 v.php 的php文件。当我浏览 domain.com/v.php 时,它显示一个搜索框..当我输入一个关键字并按下搜索按钮时,只显示一个空白页面。我错过了什么?我很困惑,不知道该怎么做。 *我尝试了下面图像的api(服务器和浏览器)。 这是片段。
<?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']) {
// Call set_include_path() as needed to point to your client library.
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
/*
* 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 = '39 digit api key';
$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_Service_Exception $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>