Easy way to get Vimeo id from a vimeo url

你说的曾经没有我的故事 提交于 2021-02-04 09:14:45

问题


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);

回答1:


I think using parse_url() is the best option:

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

echo (int) substr(parse_url($vimeo, PHP_URL_PATH), 1);



回答2:


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




回答3:


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



回答4:


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




回答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];



回答6:


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.



回答7:


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];
    }
}



回答8:


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

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




回答9:


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



来源:https://stackoverflow.com/questions/10488943/easy-way-to-get-vimeo-id-from-a-vimeo-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!