Check if Youtube and Vimeo-clips are valid

前端 未结 5 2093
花落未央
花落未央 2020-12-05 15:44

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.

Any ideas how to check ur

相关标签:
5条回答
  • 2020-12-05 16:30

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

    /**
     * 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.
    }
    
    0 讨论(0)
  • 2020-12-05 16:32

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

    0 讨论(0)
  • 2020-12-05 16:33
    /* 
     * 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;
        }
    
    0 讨论(0)
  • 2020-12-05 16:36

    If you check the response headers from a request to http://gdata.youtube.com/feeds/api/videos/videoId where 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.

    // 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;
    }
    
    0 讨论(0)
  • 2020-12-05 16:37

    i see a answer in this site : 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:

    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 :

    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/FLE2htv9oxc if 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.

    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

    0 讨论(0)
提交回复
热议问题