我正在尝试使用 youtube api V3 验证 youtube 视频(带有 id_video)是否有效/存在。这就是我所做的(y2oy7b4SFgE 是我测试的视频的 ID):
$file_headers = @get_headers('https://www.googleapis.com/youtube/v3/videos?part=id&id=y2oy7b4SFgE&key=ma_clé_api_publique');
//exit(var_dump($file_headers));
if ($file_headers[0] == 'HTTP/1.0 200 OK') {
$resultat = $file_headers[0].'Goood'
} else {
$resultat = $file_headers[0].'PasGoood'
}
但我有一个“代码”:403, "message": "在您的 API key 上配置了每个 IP 或每个 Referer 的限制,并且请求不符合这些限制。如果来自此 IP 或 Referer 的请求应该使用 Google Developers Console 更新您的 API key 配置被允许。”
它在没有 referer 的情况下运行良好。但是当我放一个时,我尝试使用我的网站名称或我的 vps 服务器的 ip,但每次都不起作用。
最佳答案
我知道有两种方法可以使用 YouTube 视频 ID 检查视频是否存在。
最简单的方法是使用视频的嵌入网址。这不需要身份验证,并在视频无效时返回 404 header 。您真的应该使用 curl 来执行此操作,因为根据您的设置,file_get_contents 可能不适合您...
$headers = get_headers('https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=' . $videoID);
if(is_array($headers) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$headers[0]) : false){
// video exists
} else {
// video does not exist
}
第二种是使用V3 of the data api .要使用此方法,您需要在开发人员控制台中生成一个 API key 。
$response = file_get__contents('https://www.googleapis.com/youtube/v3/videos?part=id&id=' . $videoID . '&key=' . $apiPublicKey);
$json = json_decode($response);
if (sizeof($json['items'])) {
// video exists
} else {
// video does not exist
}
关于php - 使用 youtube api v3 验证视频是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29166402/