【发布时间】:2021-02-01 07:59:02
【问题描述】:
我在使用 YouTube 数据 API 时遇到问题。我正在开发一个 PHP 函数,以从 YouTube 频道中检索 JSON 格式的所有视频信息,然后将其放入文件中。
到目前为止,我设法调用了 API 并检索了前 50 个视频信息;我也知道创建文件的方法。
我知道我必须使用nextPageToken 参数循环该函数,直到没有nextPageToken,但我卡住了。我看过很多类似的帖子解决这个问题,但没有一个真正帮助我。
综上所述,我需要帮助:
- 使用
nextPageToken收集50个视频后的信息; - 在循环的每一圈收集此信息。
到目前为止我找到并使用的代码:
function youtube_search($API_key, $channelID, $max_results, $next_page_token=''){
$myQuery = "https://www.googleapis.com/youtube/v3/search?key=".$API_key."&channelId=".$channelID."&part=snippet,id&order=date&maxResults=".$max_results;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $myQuery);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
if(!empty($data->nextPageToken)){
return youtube_search($API_key, $channelID, $max_results, $searchResponse['nextPageToken']);
}
}
youtube_search($API_key, $channelID, $max_results, $next_page_token='');
感谢您的帮助(我是一个谦虚的初学者-_-)。
【问题讨论】:
-
更具体地说,当您尝试此代码时出了什么问题?
-
$searchResponse未定义,因此$searchResponse['nextPageToken']将不起作用。您将$next_page_token作为函数的参数,但您根本不使用该变量。 -
"在循环的每一圈收集这些信息" 什么循环?你的函数似乎也没有返回任何可以使用的东西,你正在得到
$data但没有用它做任何事情。
标签: php youtube-api youtube-data-api