I am working on project need to get last video from many channels in Youtube
and store it in mysql now I work in file it is work by cron job every on hours to check last video , the problem now I have more than 30 channels and I use curl to get last video in every channel ,you can see the code
foreach ($channels as $channel){
$channel_id = $channel['channel_id'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=$channel_id&maxResults=50&order=date&key=$key" ,
CURLOPT_RETURNTRANSFER => true
));
$json = curl_exec($curl);
$data_json = json_decode($json , TRUE);
$item = $data_json['items'][0];
// complete code here to check last video
}
now my qustion is:
1- is it good idea or there are better?
2 - are there any problem to use many request Curl in youtube api? Thanks
YouTube API v2 supported batch operations; API v3 no longer does. You could do try to run multiple requests in parallel using curl_multi_init()
.
If you only care about one item per request, don't request maxResults=50
.
Do all of the videos have something else in common? For example, are the all uploaded by the authenticated user? If so, try searching for videos uploaded by that user (forMine=true
), publishedAfter
the time of the previous cron job. Then triage the results according to each video's channelId
.