我在 PHP 中使用这段代码来获取 Youtube 服务器 URL
<!-- language: php -->
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{
"videoId": "'.$yt_video_id.'",
"context": {
"client": {
"clientName": "ANDROID",
"clientVersion": "16.20.35"
},
"device": {
"platform": "ANDROID",
"platformVersion": "11"
}
},
"playbackMode": "EMBEDDED",
"playerParams": {
"autoplay": 1
}
}');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo '<pre>';print_r($result);
它工作正常并返回视频信息以及格式和自适应格式。返回的服务器 URL 在 URL 本身中也有过期字段,这意味着返回的媒体 URL 将在该时间后过期。
我添加了在数据库中保存过期的逻辑,并在当前时间大于之前的过期时间时重新运行上面的代码以获取新的媒体 URL。
问题是上面的curl请求有时会返回以下错误,但是当我刷新时,它起作用了:
<!-- language: json -->
"reason": {
"runs": [
{
"text": "The following content is not available on this app."
}
]
},
恐怕我的直播视频会发生这种情况,用户将无法观看视频。
我做错了什么,处理过期的最佳方法是什么?我在这里错过了什么?我已经看到许多其他视频播放器工作正常,没有错误。
不是 100% 确定,但如果您使用通用 apikey,被随机阻止的机会非常高。尝试申请常规密钥,如果您经常达到 10000 限制,请申请增加配额。
有了通用密钥的可靠性,我们谁也不会使用我们自己的个人数据,实际上使他们的控制程序无效。