我试图搜索类似的东西,但没有找到任何相关的东西。我的问题是我尝试制作,因此当您搜索视频时,它第一次仅显示 8 个视频,然后每次单击"更多视频"时,它都会添加 8 个视频。猜测我在"maxResults"或/和"pageToken"上做错了什么。如果我在版本 3 中正确理解了 API,您只能显示 50 个视频,然后您需要转到另一个页面,有没有办法让它继续向下?任何帮助,不胜感激。
<?php
$htmlBody = "";
// 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.
$index_playlist = '<iframe width="300" height="180" src="//www.youtube.com/embed/videoseries?list=PLn1aPDOSlmD8vLeP8S2KDogDcmCgtTmHg" frameborder="0" allowfullscreen></iframe>';
if ($_GET['q']) {
$index_playlist = '';
// Call set_include_path() as needed to point to your client library.
require_once 'google_analytics/src/Google_Client.php';
require_once 'google_analytics/src/contrib/Google_YouTubeService.php';
/*
* Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the
* Google Cloud Console <https://cloud.google.com/console>
* Please ensure that you have enabled the YouTube Data API for your project.
*/
$DEVELOPER_KEY = 'HIDDEN_FOR_THIS_POST';
$client = new Google_Client();
$client->setDeveloperKey($DEVELOPER_KEY);
// Define an object that will be used to make all API requests.
$youtube = new Google_YouTubeService($client);
$result = 8;
$results = $results + $result;
try {
// Call the search.list method to retrieve results matching the specified
// query term.
if ($_GET['more']) {
$searchResponse = $youtube->search->listSearch('id,snippet', array(
'q' => $_GET['q'],
'pageToken' => $_GET['CAgQAA'],
'maxResults' => $result,
'type' => 'video',
));
} else {
$searchResponse = $youtube->search->listSearch('id,snippet', array(
'q' => $_GET['q'],
'maxResults' => $results,
'type' => 'video',
));
};
//echo $searchResponse['nextPageToken'];
//exit;
$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':
$htmlBody .= '<iframe width="300" height="180" src="//www.youtube.com/embed/' . $searchResult['id']['videoId'] . '" frameborder="0" allowfullscreen style="margin-right:30px; margin-bottom:30px"></iframe>';
// $videos .= sprintf('<li>%s</li>',
// $videos .= sprintf('<li><a href="%s"</a></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;
*/
};
};
// <ul>$videos</ul>
// <a href="<ul>$videos</ul>"<a/>
/* $htmlBody .= <<<END
<h3>Videos</h3>
<a href="<ul>$videos</ul>"</a>
<a href="<ul>$playlists</ul>"</a>
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>TubeSearch</title>
</head>
<body>
<form method="GET">
<div>
TubeSearch:
<br>
<input type="search" id="q" name="q" placeholder="Enter Search Term">
</br>
</div>
<input type="submit" value="Search">
</form>
<?php if (isset($_GET['q'])) : ?>
<form method="GET">
<input type="hidden" name="q" value="<?php echo $_GET['q']; ?>">
<input type="hidden" name="more" value="<?php echo $_GET['q']; ?>">
<input type="submit" name="more" value="More Videos">
</form>
<?php endif; ?>
<?=$htmlBody?>
<?=$index_playlist?>
</body>
</html>
是的,CAqQAA
是从 Youtube 返回的字符串。你把它放在$_GET
数组中,在那里它找不到它。您必须使用$searchResponse['nextPageToken']
进行pageToken
.
您可以将其作为hidden
值的形式放入<input type="hidden" name="token" value="<?php echo $searchResponse['nextPageToken']; ?>"/>
然后在你的第二个,如果你能做'pageToken' => $_GET['token']