I wrote a PHP script to monitor my own channel every 10 minutes (cron). If there is a live event, it will grab the video id and title and write file(s) to be read by my homepage, that will read info and embed the videos. No live event, it will delete any existing file.
Works, but sometimes it returns ok, and sometimes returns empty (while I'm broadcasting).
API key is ok, and I'm not exceeding the quotas (day/minute/user). It worked fine for some days and now it's unstable.
$API_KEY='--my-key--';$ChannelID='UCIkAO7iHS6R9AtcABE9MdRQ';$channelInfo='https://www.googleapis.com/youtube/v3/search?part=snippet&channelId='.$ChannelID.'&type=video&eventType=live&key='.$API_KEY;$extractInfo=file_get_contents($channelInfo);if (!$extractInfo) exit;$extractInfo=str_replace('},]',"}]",$extractInfo);$showInfo=json_decode($extractInfo,true);if ($showInfo['pageInfo']['totalResults']==0) { foreach (glob('youtube-live/*') as $f) unlink($f);} else { foreach ($showInfo['items'] as $s) file_put_contents('youtube-live/'.$s['id']['videoId'],utf8_decode($s['snippet']['title']));}
So, it writes a file per live video: the filename is the video id and inside it the video title. My /index.php read the folder, if any files found, read them and embed.
For testing, I'm running and displaying $extractInfo:
[root@osiris scripts]# php youtube-live.php{"kind": "youtube#searchListResponse","etag": "pxWq-53iYggSAuHGuffJgVUk25A","regionCode": "US","pageInfo": {"totalResults": 1,"resultsPerPage": 1 },"items": [ {"kind": "youtube#searchResult","etag": "szhtFd3s3W5cHGpGpn838qX-eWU","id": {"kind": "youtube#video","videoId": "aUvYe9hYKMs" },"snippet": {"publishedAt": "2021-06-30T23:53:30Z","channelId": "UCIkAO7iHS6R9AtcABE9MdRQ","title": "ESTÃDIO 97 - AO VIVO - 01/07/21","description": "INSCREVA-SE NO CANAL: http://youtube.com/energia97","thumbnails": {"default": {"url": "https://i.ytimg.com/vi/aUvYe9hYKMs/default_live.jpg","width": 120,"height": 90 },"medium": {"url": "https://i.ytimg.com/vi/aUvYe9hYKMs/mqdefault_live.jpg","width": 320,"height": 180 },"high": {"url": "https://i.ytimg.com/vi/aUvYe9hYKMs/hqdefault_live.jpg","width": 480,"height": 360 } },"channelTitle": "Energia 97 FM","liveBroadcastContent": "live","publishTime": "2021-06-30T23:53:30Z" } } ]}
Ok! I'm live! Then I wait 2 (or 5 or 10) minutes and run again:
[root@osiris scripts]# php youtube-live.php{"kind": "youtube#searchListResponse","etag": "UBAOiXjvY0CZ7Ub9tJDO9bUtsU8","regionCode": "US","pageInfo": {"totalResults": 0,"resultsPerPage": 0 },"items": []}
And I'm stuck here. No quota problems and broadcasting ok, for sure. Any ideas?