30

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);
Wakenn
  • 341
  • 1
  • 4
  • 12

9 Answers9

47

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...

Darryl Hein
  • 134,677
  • 87
  • 206
  • 257
user2200660
  • 1,181
  • 3
  • 17
  • 23
  • 19
    To make this friendly for PHP's preg_match, I simply escaped the forward slashes and dots: `'/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/'` – Morgan Delaney Apr 08 '14 at 16:45
  • 3
    I made a recap of those solutions to get ids from vimeo, and also youtube and dailymotion, here: https://github.com/lingtalfi/video-ids-and-thumbnails/blob/master/testvideo.php. – ling Sep 28 '15 at 20:31
  • 1
    Note: This regex is no longer valid, and does not cover additional problems associated with the "unlisted" privacy mode. Please see http://stackoverflow.com/a/34027757/1886079 for a thorough, future proof solution. – Dashron Jan 08 '18 at 15:33
  • I have a different Vimeo URL (https://vimeo.com/355415017/1b3271f41f). What is the id of this URL? – Poonkodi Mar 20 '21 at 06:44
35

I think using parse_url() is the best option:

$vimeo = 'https://vimeo.com/29474908';

echo (int) substr(parse_url($vimeo, PHP_URL_PATH), 1);
Wouter J
  • 39,482
  • 15
  • 97
  • 108
  • Note: This answer is no longer valid, and does not cover additional problems associated with the "unlisted" privacy mode. Please see http://stackoverflow.com/a/34027757/1886079 for a thorough, future proof solution. – Dashron Jan 08 '18 at 15:33
25

For those of you who want to see the code fully implemented using PHP, I am using the regex provided by user2200660 and formatted for PHP by Morgan Delaney, here it is:

$vimeo = 'http://player.vimeo.com/video/67019023';

if(preg_match("/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/", $vimeo, $output_array)) {
    echo "Vimeo ID: $output_array[5]";
}

//outputs: Vimeo ID: 67019023
zeckdude
  • 14,313
  • 39
  • 126
  • 173
  • Note: This regex is no longer valid, and does not cover additional problems associated with the "unlisted" privacy mode. Please see https://stackoverflow.com/a/34027757/1886079 for a thorough, future proof solution. – Dashron Jan 08 '18 at 15:33
  • 1
    sometimes need to receive IDs from a list of your own xx URLs without sending requests to Vimeo. – Intacto Aug 21 '18 at 22:22
  • this is still valid but unfortunately , i always get error failed to open stream: No connection could be made because the target machine actively refused it. looks like it's been blocked by their firewall. – tyo Dec 21 '20 at 18:02
15

[Edit] You can now do this all via the API!

If you provide a comma separated list of your Vimeo urls via the "links" parameter to the search endpoint (https://developer.vimeo.com/api/endpoints/videos#GET/videos) we will return those videos as API responses.

e.g.

GET https://api.vimeo.com/videos?links=https://vimeo.com/74648232,https://vimeo.com/232323497

[Original]

Vimeo provides many different type of video urls, some of which do not include the id. To ensure support across all of Vimeo's urls you should ask vimeo directly for the ID.

You can ask vimeo via the oEmbed endpoint.

There are many options, but the easiest option is to make an HTTP GET request to the url https://vimeo.com/api/oembed.json?url={vimeo_url}, replacing {vimeo_url} with the appropriate url.

For example, to get the ID of the url you provided above (https://vimeo.com/29474908) make an HTTP GET request to

https://vimeo.com/api/oembed.json?url=https://vimeo.com/29474908

Parse the JSON response, and grab the video_id parameter.

Dashron
  • 3,781
  • 2
  • 12
  • 21
5

This should retrieve the ID from all kinds of vimeo urls.

$url = 'https://vimeo.com/cool/29474908?title=0&byline=0&portrait=0';
$urlParts = explode("/", parse_url($url, PHP_URL_PATH));
$videoId = (int)$urlParts[count($urlParts)-1];
Steve
  • 84
  • 1
  • 2
4

A current, working regex:

function getIdFromVimeoURL(url) {
  return /(vimeo(pro)?\.com)\/(?:[^\d]+)?(\d+)\??(.*)?$/.exec(url)[3];
}

console.log(getIdFromVimeoURL("https://vimeo.com/channels/staffpicks/272053388"))
console.log(getIdFromVimeoURL("https://vimeo.com/272053388"))
console.log(getIdFromVimeoURL("https://player.vimeo.com/video/272053388"))

// ...etc.
Raphael Rafatpanah
  • 15,663
  • 19
  • 73
  • 136
1

If someone need it in JavaScript based on @user2200660 answer:

function getVimeoVideoId(url){

    var regex = new RegExp(/(https?:\/\/)?(www.)?(player.)?vimeo.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/);

    if ( regex.test(url) ) {
        return regex.exec(url)[5];
    }
}
Roy Shoa
  • 2,677
  • 1
  • 31
  • 39
0

If you only need the Vimeo ID, you can use the RegExp non-capturing groups:

(?:https?:\/\/)?(?:www\.)?vimeo\.com\/(?:(?:[a-z0-9]*\/)*\/?)?([0-9]+)

  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – Dwhitz Apr 09 '19 at 09:33
0

A lot of good answers here, specifically @user2200660.

https://stackoverflow.com/a/16841070/3850405

However a use case that has not been supported in the previous answers is this:

https://vimeo.com/showcase/7008490/video/407943692

Regex that can handle it and the other examples:

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

https://regex101.com/r/p2Kldc/1/

$vimeo = 'http://player.vimeo.com/video/67019023';

if(preg_match("/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/?(showcase\/)*([0-9))([a-z]*\/)*([0-9]{6,11})[?]?.*/", $vimeo, $output_array)) {
    echo "Vimeo ID: $output_array[6]";
}

Credits to @zeckdude for the original example code in PHP.

https://stackoverflow.com/a/29860052/3850405

Ogglas
  • 38,157
  • 20
  • 203
  • 266