检查的Youtube和Vimeo剪辑是有效的(Check if Youtube and Vimeo-

2019-07-04 05:43发布

我一直在尝试了好一阵子,以检查是否提交链接访问youtube.com或vimeo.com有效的电影剪辑,但我没有成功。

任何想法如何检查的URL,如:

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)

我使用PHP。

Answer 1:

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

他说:

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

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

检查你将需要提取“V”值,并发送包含视频ID来请求视频的存在:

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

其中,视频ID是例如“V”值视频FLE2htv9oxc将被查询这样http://gdata.youtube.com/feeds/api/videos/FLE2htv9oxc如果它不存在,那么你会得到与“无效ID的页面“如果存在的话,将返回其有关视频的各种信息的XML Feed。 这样你可以检查该视频存在。

希望这将让你在正确的方向。

与VIMEO同样的事情,尝试API文档中查找在那里的网站。 http://www.vimeo.com/api



Answer 2:

如果你从一个请求检查响应头http://gdata.youtube.com/feeds/api/videos/videoId其中VideoID的是谷歌的视频标识符,你应该得到一个200,如果视频中存在和400(坏请求)如果视频不存在。

// 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;
}


Answer 3:

我写此功能检查,如果该链接是一个有效的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.
}


Answer 4:

你可以尝试捕捉301头,你管抛出如果视频不再有效



Answer 5:

/* 
 * 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;
    }


文章来源: Check if Youtube and Vimeo-clips are valid
标签: php url