Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to get Vimeo id from a vimeo url

Tags:

regex

php

vimeo

I'm trying to get just the id from a vimeo URL. Is there a simpler way than this? All the vimeo video urls I see are always:

https://vimeo.com/29474908

https://vimeo.com/38648446

// VIMEO   $vimeo = $_POST['vimeo'];  function getVimeoInfo($vimeo) {     $url = parse_url($vimeo);     if($url['host'] !== 'vimeo.com' &&             $url['host'] !== 'www.vimeo.com')         return false;    if (preg_match('~^http://(?:www\.)?vimeo\.com/(?:clip:)?(\d+)~', $vimeo, $match))     {        $id = $match[1];    }    else    {        $id = substr($link,10,strlen($link));    }     if (!function_exists('curl_init')) die('CURL is not installed!');    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL, "http://vimeo.com/api/v2/video/$id.php");    curl_setopt($ch, CURLOPT_HEADER, 0);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    curl_setopt($ch, CURLOPT_TIMEOUT, 10);    $output = unserialize(curl_exec($ch));    $output = $output[0];    curl_close($ch);    return $output['id']; }  $vimeo_id = getVimeoInfo($vimeo); 
like image 342
Wakenn Avatar asked May 07 '12 20:05

Wakenn


People also ask

What is my Vimeo URL?

The format of custom video URLs is vimeo.com/username/thecustompart. For example, a video on our staff account about the Vimeo player has the custom URL: vimeo.com/staff/player.

How do I use iframe for Vimeo?

If you are signed in on Vimeo, an go to your video settings, there should be an menu "Embed". Click this item and you will get various embedding options. At the right side of the window (below your profile icon) there will be a button Embed code . Clicking this will give you the Embed code.


2 Answers

There are lot many vimeo URLs that are valid. Few examples are

All valid URLs:

http://vimeo.com/6701902 http://vimeo.com/670190233 http://player.vimeo.com/video/67019023 http://player.vimeo.com/video/6701902 http://player.vimeo.com/video/67019022?title=0&byline=0&portrait=0 http://player.vimeo.com/video/6719022?title=0&byline=0&portrait=0 http://vimeo.com/channels/vimeogirls/6701902 http://vimeo.com/channels/vimeogirls/67019023 http://vimeo.com/channels/staffpicks/67019026 http://vimeo.com/15414122 http://vimeo.com/channels/vimeogirls/66882931 

All invalid URLs:

http://vimeo.com/videoschool http://vimeo.com/videoschool/archive/behind_the_scenes http://vimeo.com/forums/screening_room http://vimeo.com/forums/screening_room/topic:42708 

I wrote this java regex that catches all the above valid URLs and rejects the invalid ones. I m not sure though if they vimeo has more valid URLs.

(https?://)?(www.)?(player.)?vimeo.com/([a-z]*/)*([0-9]{6,11})[?]?.* 

Hope this helps...

like image 93
user2200660 Avatar answered Sep 18 '22 21:09

user2200660


I think using parse_url() is the best option:

$vimeo = 'https://vimeo.com/29474908';  echo (int) substr(parse_url($vimeo, PHP_URL_PATH), 1); 
like image 31
Wouter J Avatar answered Sep 21 '22 21:09

Wouter J