我正在Youtube Data Api V3上进行数据库播种。一切都进行得很好,直到我尝试从所有使用“ nextPageToken”的页面获取数据为止。
这是我的代码,
class YoutubeSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// Playlist 1
$client = new Client();
$res1 = $client->request('GET', 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId={PLAYLIST_ID}&key={API_KEY}');
$result = json_decode($res1->getBody(),true); // Nak return api data dalam bentuk JSON
$datas = $result['items'];
// Playlist 2
$client2 = new Client();
$res2 = $client2->request('GET', 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId={PLAYLIST_ID}&key={API_KEY}');
$result2 = json_decode($res2->getBody(),true); // Nak return api data dalam bentuk JSON
$datas2 = $result2['items'];
foreach (array_merge($datas, $datas2) as $data) {
$client = new Client();
$res3 = $client->request('GET', 'https://www.googleapis.com/youtube/v3/videos?fields=items(id,snippet(publishedAt,title,description,thumbnails(medium)),contentDetails(duration),statistics(viewCount))&part=snippet%2CcontentDetails%2Cstatistics&key={API_KEY}&id='.$data['snippet']['resourceId']['videoId']);
$result = json_decode($res3->getBody(),true); // Nak return api data dalam bentuk JSON
$datasdetails = $result['items'];
foreach ($datasdetails as $datadetails) {
Youtube::updateOrCreate(
[
'video_id' => $data['snippet']['resourceId']['videoId'],
],
[
'title' => $data['snippet']['title'],
'date_published' => $data['snippet']['publishedAt'],
'thumbnails' => $data['snippet']['thumbnails']['medium']['url'],
'description' => $data['snippet']['description'],
'duration' => $datadetails['contentDetails']['duration'],
'views' => $datadetails['statistics']['viewCount'],
'playlist' => $data['snippet']['playlistId'],
'tags' => $data['snippet']['resourceId']['videoId'],
]
);
}
}
}
}
根据我的代码,它表明我正在尝试获取视频数据并将其导入到mysql db中,并且运行良好。但是,它仅从第一页获取数据。如何仅使用一个种子使用“ nextPageToken”从所有页面获取数据?
我尝试了很多方法,但没有给我任何解决方案。