问题描述
我想验证一个YouTube视频(与id_video)有效/存在,使用YouTube API V3。这就是我做的(y2oy7b4SFgE是视频的ID我测试):
I'm trying to verify if a youtube video (with the id_video) is valid/exist , using the youtube api V3. That's what i do (y2oy7b4SFgE is the id of the video i test):
$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'
}
但我有一个code:403,
消息:有一个每个IP或每个引用站点上的API密钥配置限制和要求不符合这些限制,请使用谷歌开发者控制台,如果从这个IP或引荐请求应该更新您的API密钥配置。被允许。
But i have a "code": 403,"message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
其工作时,也没有参考。但是当我把一个,我试图用我的网站的名称,或者用我的VPS服务器,每次doesn'work时的IP地址。
Its working well when no referer. But when i put one, i tried with the name of my website or with the ip of my vps server, each time it doesn'work.
推荐答案
有两种方式我知道的检查,如果视频使用YouTube视频ID存在。
There are two ways I am aware of to check if a video exists using the YouTube video id.
,最简单的是使用透过oEmbed网址为视频。这不需要身份验证,并在视频无效返回一个404头。你真的应该用卷曲这样做,因为根据您的设置的file_get_contents可能不是一个选择...
The simplest is to use the oembed url for the video. This requires no authentication and returns a 404 header when the video is invalid. You should really be doing this with curl, as depending on your setup file_get_contents may not be an option for you...
$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
}
二是要使用的数据API 最近发布的 V3。要使用这个方法,你需要生成在开发者控制台API密钥。
The second is to use the recently released V3 of the data api. To use this method you will need to generate an api key in the developer console.
$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
}
这篇关于验证是否视频与YouTube API V3存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!