php 检查 Youtube 和 Vimeo-clips 是否有效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1362345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:12:11  来源:igfitidea点击:

Check if Youtube and Vimeo-clips are valid

phpurl

提问by Johan

I have been trying for quite a while to check if submitted links are valid film-clips from youtube.com or vimeo.com but I didn't succeed.

我一直在尝试检查提交的链接是否是来自 youtube.com 或 vimeo.com 的有效电影剪辑,但我没有成功。

Any ideas how to check url's like:

任何想法如何检查网址,如:

http://www.youtube.com/watch?v=jc0rnCBCX2c&feature=fvhl (valid)
http://www.youtube.com/watch?v=jc0FFCBCX2c&feature=fvhl (not valid)
http://www.youtube.com/v/jc0rnCBCX2c (valid)
http://www.youtube.com/v/ddjcddddX2c (not valid)
http://www.vimeo.com/463l522 (not valid)
http://www.vimeo.com/1483909 (valid)
http://www.vimeo.com/lumiblue (not valid)
http://www.youtube.com/user/dd181921 (not valid)

?

?

I use php.

我使用php。

采纳答案by Haim Evgi

i see a answer in this site : www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23765374.html

我在这个网站上看到了一个答案:www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23765374.html

and he said :

他说:

I would suggest using youtube's API since you are trying to validate if the video exists. or if you don't want to go into API stuff then you can do simple trick. check this link:

我建议使用 youtube 的 API,因为您正在尝试验证视频是否存在。或者如果你不想进入 API 的东西,那么你可以做一些简单的技巧。检查此链接:

http://code.google.com/apis/youtube/developers_guide_php.html#RetrievingVideoEntry

http://code.google.com/apis/youtube/developers_guide_php.html#RetrievingVideoEntry

to check for the existence of a video you will need to extract "v" value and send a request that contains the video id to :

要检查视频是否存在,您需要提取“v”值并将包含视频 ID 的请求发送到:

http://gdata.youtube.com/feeds/api/videos/videoID

http://gdata.youtube.com/feeds/api/videos/videoID

where videoID is the "v" value for example a video FLE2htv9oxc will be queried like this http://gdata.youtube.com/feeds/api/videos/FLE2htv9oxcif it does not exist then you will get a page with "Invalid id" if it exists, will return an XML feed having various info about the video. this way you can check that the video exists.

其中 videoID 是“v”值,例如视频 FLE2htv9oxc 将像这样查询 http://gdata.youtube.com/feeds/api/videos/FLE2htv9oxc如果它不存在,那么您将获得一个带有“无效 ID”的页面" 如果存在,将返回包含有关视频的各种信息的 XML 提要。这样您就可以检查视频是否存在。

hope this will get you in the right direction.

希望这会让你朝着正确的方向前进。

the same thing with vimeo , try to look in api documentation in there site. http://www.vimeo.com/api

与 vimeo 相同,请尝试查看该站点中的 api 文档。 http://www.vimeo.com/api

回答by chrismacp

If you check the response headers from a request to http://gdata.youtube.com/feeds/api/videos/videoIdwhere videoId is the google video Identifier, you should get a 200 if the video exists and a 400 (bad request) if the video doesn't exist.

如果您检查来自对http://gdata.youtube.com/feeds/api/videos/videoId的请求的响应标头,其中 videoId 是谷歌视频标识符,如果视频存在,您应该得到 200(错误请求) ) 如果视频不存在。

// PHP code

// Check if youtube video item exists by the existance of the the 200 response
$headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $youtubeId);
if (!strpos($headers[0], '200')) {
    echo "The YouTube video you entered does not exist";
    return false;
}

回答by jason

I wrote this function to check if the link is a valid youtube link.

我编写了这个函数来检查链接是否是有效的 youtube 链接。

/**
 * This function will check if 'url' is valid youtube video and return the ID.
 *  If the return value === false then this is **not** a valid youtube url, otherwise   the youtube id is returned.
 *
 * @param <type> $url
 * @return <type>
 */


 private static function get_youtube_id($url) {
        $link = parse_url($url,PHP_URL_QUERY);

    /**split the query string into an array**/
    if($link == null) $arr['v'] = $url;
    else  parse_str($link, $arr);
    /** end split the query string into an array**/
    if(! isset($arr['v'])) return false; //fast fail for links with no v attrib - youtube only

    $checklink = YOUTUBE_CHECK . $arr['v'];

    //** curl the check link ***//
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$checklink);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $result = curl_exec($ch);
    curl_close($ch);

    $return = $arr['v'];
    if(trim($result)=="Invalid id") $return = false; //you tube response

    return $return; //the stream is a valid youtube id.
}

回答by MANCHUCK

You can try to catch the 301 header that you tube throws if the video is no longer valid

如果视频不再有效,您可以尝试捕获您管抛出的 301 标头

回答by Carca

/* 
 * Verify YouTube video status
 */

    $videoID = "o8UCI7r1Aqw";
    $header = get_headers("http://gdata.youtube.com/feeds/api/videos/". $videoID);

    switch($headers[0]) {
    case '200':
    // video valid
    break;

    case '403':
    // private video
    break;

    case '404':
    // video not found
    break;

    default:
    // nothing  above
    break;
    }