32

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

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

9 回答 9

48

有很多有效的 vimeo 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

所有无效网址:

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

我编写了这个 java 正则表达式,它捕获所有上述有效 URL 并拒绝无效 URL。我不确定他们 vimeo 是否有更多有效的 URL。

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

希望这可以帮助...

于 2013-05-30T16:12:01.380 回答
38

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

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

echo (int) substr(parse_url($vimeo, PHP_URL_PATH), 1);
于 2012-05-07T20:56:08.187 回答
26

对于那些想要查看使用 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
于 2015-04-25T01:45:11.287 回答
15

[编辑]您现在可以通过 API 完成这一切!

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

例如

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

[原来的]

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

您可以通过oEmbed 端点询问 vimeo 。

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

例如,要获取您在上面提供的 URL 的 ID ( https://vimeo.com/29474908 ),请发出 HTTP GET 请求

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

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

于 2015-12-01T18:45:00.780 回答
5

这应该从各种 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];
于 2015-04-14T21:19:06.143 回答
5

当前有效的正则表达式:

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.

于 2018-06-01T21:48:44.420 回答
2

如果有人需要它基于@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];
    }
}
于 2015-12-24T09:30:25.500 回答
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]+)

于 2019-04-09T09:11:44.387 回答
0

这里有很多很好的答案,特别是@user2200660。

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

但是,以前的答案中不支持的用例是:

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

可以处理它的正则表达式和其他示例:

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

感谢 @zeckdude 提供 PHP 中的原始示例代码。

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

于 2021-01-22T13:16:15.687 回答