Check if Youtube and Vimeo-clips are valid

前端 未结 5 2105
花落未央
花落未央 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  $url
     * @return 
     */
    
    
     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.
    }
    

提交回复
热议问题