我写了一个 PHP 脚本来每 10 分钟监控我自己的频道(cron)。如果有现场活动,它将获取视频 ID 和标题并写入要由我的主页读取的文件,该文件将读取信息并嵌入视频。没有实时事件,它将删除任何现有文件。
有效,但有时返回正常,有时返回空(当我正在广播时)。
API 密钥没问题,我没有超过配额(天/分钟/用户)。它运行了好几天,现在不稳定。
$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']));
}
因此,它为每个直播视频写入一个文件:文件名是视频 ID,其中包含视频标题。我的 /index.php 读取文件夹,如果找到任何文件,读取它们并嵌入。
为了测试,我正在运行并显示 $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"
}
}
]
}
好的!我活着!然后我等待 2(或 5 或 10)分钟并再次运行:
[root@osiris scripts]# php youtube-live.php
{
"kind": "youtube#searchListResponse",
"etag": "UBAOiXjvY0CZ7Ub9tJDO9bUtsU8",
"regionCode": "US",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 0
},
"items": []
}
我被困在这里了。没有配额问题,广播没问题,这是肯定的。有什么想法吗?