Youtube, Vimeo and Dailymotion video detection with PHP

Yepkoo

Yepkoo
Staff member
Yes, friends, with the function defined below, you can read Youtube, Video and Dailymotion videos only with the url address and print them in html format automatically.

PHP:
// PHP Youtube Thumbnail Image read

function getYouTubeThumbnailImage($video_id,$vidimgbig='large')
{
    if ($vidimgbig == 'small') { $read_imgsize = 'mqdefault'; } elseif ($vidimgbig == 'medium') { $read_imgsize = 'sddefault'; } else { $read_imgsize = 'hqdefault'; }
    return "https://i.ytimg.com/vi/$video_id/".$read_imgsize.".jpg"; //pass 0,1,2,3 for different sizes like 0.jpg, 1.jpg -  default,hqdefault,mqdefault,sddefault,maxresdefault
}

PHP:
// PHP Vimeo Thumbnail Image read
function getVimeoThumb($id,$vidimgbig='large')
{
    $arr_vimeo = unserialize(@file_get_contents("https://vimeo.com/api/v2/video/$id.php"));
    if ($vidimgbig == 'small')
    {
        $check_vimeoth = $arr_vimeo[0]['thumbnail_small'];
    }
    elseif ($vidimgbig == 'medium')
    {
        $check_vimeoth = $arr_vimeo[0]['thumbnail_medium'];
    }
    else
    {
        $check_vimeoth =  $arr_vimeo[0]['thumbnail_large'];
    }
    $exp_vimeo = explode('_640',$check_vimeoth);
    return $exp_vimeo[0].'.jpg';
}

PHP:
// PHP Dailymotion Thumbnail Image read
function getDailymotionThumb($id,$vidimgbig='large')
{
    $thumb = '';
    if (!empty($id))
    {
        if ($vidimgbig == 'small') { $read_imgsize = 'thumbnail_360_url'; } elseif ($vidimgbig == 'medium') { $read_imgsize = 'thumbnail_480_url'; } else { $read_imgsize = 'thumbnail_720_url'; }
        $thumbnail_large_url = 'https://api.dailymotion.com/video/'.$id.'?fields='.$read_imgsize;
        $json_thumbnail = @file_get_contents($thumbnail_large_url);
        $arr_dailymotion = json_decode($json_thumbnail, TRUE);
        if (isset($arr_dailymotion))
        {
            $thumb = $arr_dailymotion[$read_imgsize];
        }
        return $thumb;
    }
}

PHP:
// PHP video tespiti fonksiyonu = PHP video detection function

function extractVideoID($url,$urlname='no',$imagesize='large')
{
    $regExp = "/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/";
    preg_match($regExp, $url, $video);
    if (isset($video[7])) { $videid_youtube = $video[7]; }

    if (!empty($videid_youtube))
    {
        if ($urlname == 'id') { $videoid = $videid_youtube; } elseif ($urlname != 'ok') { $videoid = getYouTubeThumbnailImage($videid_youtube,$imagesize); } else { $videoid = 'youtube'; }
    }
    else
    {
        $regExp1 = "/^.*((youtube.com\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/";
        preg_match($regExp1, $url, $video2);
        if (isset($video2[7])) { $videid_youtube = $video2[7]; }

        if (!empty($videid_youtube))
        {
            if ($urlname == 'id') { $videoid = $videid_youtube; } elseif ($urlname != 'ok') { $videoid = getYouTubeThumbnailImage($videid_youtube,$imagesize); } else { $videoid = 'youtube'; }
        }
        else
        {
            if (preg_match('#(?:https?://)?(?:www.)?(?:player.)?vimeo.com/(?:[a-z]*/)*([0-9]{6,11})[?]?.*#', $url, $m))
            {
                $videid_vimeo = $m[1];
            }
            if (!empty($videid_vimeo))
            {
                if ($urlname == 'id') { $videoid = $videid_vimeo; } elseif ($urlname != 'ok') { $videoid = getVimeoThumb($videid_vimeo,$imagesize); } else { $videoid = 'vimeo'; }
            }
            else
            {
                if (preg_match('!^.+dailymotion\.com/(video|hub)/([^_]+)[^#]*(#video=([^_&]+))?|(dai\.ly/([^_]+))!', $url, $m))
                {
                    if (isset($m[6])) {
                        $videid_daily = $m[6];
                    }
                    if (isset($m[4])) {
                        $videid_daily = $m[4];
                    }
                    $videid_daily = $m[2];
                }
                if (!empty($videid_daily))
                {
                    if ($urlname == 'id') { $videoid = $videid_daily; } elseif ($urlname != 'ok') { $videoid = getDailymotionThumb($videid_daily,$imagesize); } else { $videoid = 'daily'; }
                }
                else
                {
                    $slice=explode('/',$url);
                    $videid_daily=strtok(basename($url), '_');
                      
                    if (!empty($videid_daily))
                    {
                        if ($urlname == 'id') { $videoid = $videid_daily; } elseif ($urlname != 'ok') { $videoid = getDailymotionThumb($videid_daily,$imagesize); } else { $videoid = 'daily'; }
                    }
                }
            }
        }
    }
    return $videoid;
}

Create a php file and copy the above 4 functions into it.
Finally, you can create your video as follows

PHP:
//------ Final HTML output

$video_url = 'https://www.youtube.com/watch?v=xcJtL7QggTI&t=2s';
$video_imagesize = 'large'; // small,medium,large (large stable works)
$video_return = 'ok'; // id=videoid (ex. X74577X), no=video image (ex. test.jp), ok=video server name (ex. youtube)

//------ Html output with uk-lightbox
$vid_servername = extractVideoID($video_url,$video_return);

if ($vid_servername == 'daily') { $add_vidiframe = ' type="iframe"'; } else { $add_vidiframe = ''; }

echo '
<div style="min-width:70px;" id="video-gallery" uk-lightbox>
    <a href="'.$video_url.'"'.$add_vidiframe.'><img class="ui small image" src="'.extractVideoID($video_url).'" /></a>
</div>';
 
Top