从vimeo网址获取Vimeo id的简单方法

2022-08-30 13:44:40

我试图从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);

答案 1

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

所有有效网址:

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})[?]?.*

希望这有帮助...


答案 2

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

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

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

推荐