使用 youtube api v3 验证视频是否存在 [英] verify if video exist with youtube api v3

查看:24
本文介绍了使用 youtube api v3 验证视频是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 youtube api V3 验证 youtube 视频(带有 id_video)是否有效/存在.这就是我所做的(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'
}

但我有一个代码":403,"message": "在您的 API 密钥上配置了每个 IP 或每个引用者的限制,并且请求与这些限制不匹配.如果来自此 IP 或引用者的请求不符合这些限制,请使用 Google Developers Console 更新您的 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 服务器的 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 url.这不需要身份验证,并在视频无效时返回 404 标头.您真的应该使用 curl 来执行此操作,因为根据您的设置,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
}

第二种是使用V3的数据api.要使用此方法,您需要在开发者控制台中生成一个 api 密钥.

The second is to use 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 验证视频是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆