如何从"友好"网址获取vimeo id

Web*_*ars -1 php ajax vimeo

众所周知,vimeo视频通常具有以下外观:

http://vimeo.com/123456
Run Code Online (Sandbox Code Playgroud)

但昨天我看到,有些视频不同,就像这一个:

https://vimeo.com/donialiechti/stranded
Run Code Online (Sandbox Code Playgroud)

有趣的是,当您点击链接时,您会在地址栏中看到

http://vimeo.com/64334084
Run Code Online (Sandbox Code Playgroud)

此视频的嵌入代码为:

<iframe src="http://player.vimeo.com/video/64334084"... ></iframe>
Run Code Online (Sandbox Code Playgroud)

问题是我的应用需要知道视频ID才能正常工作. 那么如何将"友好"的网址转换为常规的vimeo ID?

提前致谢!

Dog*_*ert 5

Vimeo在HTTP头中发送重定向信息,您可以像这样获取它们:

function fetch_vimeo_id($url) {
    $headers = get_headers($url);
    # Reverse loop because we want the last matching header,
    # as Vimeo does multiple redirects with your `https` URL
    for($i = count($headers) - 1; $i >= 0; $i--) {
        $header = $headers[$i];
        if(strpos($header, "Location: /") === 0) {
            return substr($header, strlen("Location: /"));
        }
    }
    # Could not find id
    return null;
}

echo fetch_vimeo_id("https://vimeo.com/donialiechti/stranded");
Run Code Online (Sandbox Code Playgroud)

输出:

64334084
Run Code Online (Sandbox Code Playgroud)