php 从 vimeo url 获取 Vimeo id 的简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10488943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:18:43  来源:igfitidea点击:

Easy way to get Vimeo id from a vimeo url

phpregexvimeo

提问by Wakenn

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:

我正在尝试从 vimeo URL 中获取 id。有比这更简单的方法吗?我看到的所有 vimeo 视频网址总是:

https://vimeo.com/29474908

https://vimeo.com/29474908

https://vimeo.com/38648446

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

回答by user2200660

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

有很多有效的 vimeo URL。几个例子是

All valid URLs:

所有有效的 URL:

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.

我写了这个 java regex 来捕获所有上述有效的 URL 并拒绝无效的 URL。我不确定他们 vimeo 是否有更多有效的 URL。

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

Hope this helps...

希望这可以帮助...

回答by Wouter J

I think using parse_url()is the best option:

我认为使用parse_url()是最好的选择:

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

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

回答by zeckdude

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:

对于那些希望看到使用 PHP 完全实现的代码的人,我使用的是 user2200660 提供的正则表达式,并由 Morgan Delaney 为 PHP 格式化,这里是:

$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

回答by Dashron

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

[编辑]您现在可以通过 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.

如果您通过“链接”参数向搜索端点 ( https://developer.vimeo.com/api/endpoints/videos#GET/videos)提供 Vimeo 网址的逗号分隔列表,我们会将这些视频作为 API 响应返回.

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.

Vimeo 提供了许多不同类型的视频网址,其中一些不包含 id。为了确保支持 Vimeo 的所有 url,您应该直接向 vimeo 询问 ID。

You can ask vimeo via the oEmbed endpoint.

您可以通过oEmbed 端点询问vimeo

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.

有很多选项,但最简单的选项是向 url 发出 HTTP GET 请求https://vimeo.com/api/oembed.json?url={vimeo_url},替换{vimeo_url}为适当的 url。

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

例如,要获取您在上面提供的 url ( https://vimeo.com/29474908)的 ID,请向

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

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

Parse the JSON response, and grab the video_idparameter.

解析 JSON 响应,并获取video_id参数。

回答by Steve

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

这应该从各种 vimeo url 中检索 ID。

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

回答by Raphael Rafatpanah

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.

回答by Roy Shoa

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

如果有人需要基于 @user2200660 的 JavaScript 答案:

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

回答by Stijn Van Minnebruggen

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

如果您只需要 Vimeo ID,您可以使用 RegExp 非捕获组:

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

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