我正试图从vimeo网址获取id。有比这更简单的方法吗?我看到的所有vimeo视频网址都是:
// 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);
答案 0 :(得分:37)
有许多有效的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。我不确定他们是否有更多有效的URL。
(https?://)?(www.)?(player.)?vimeo.com/([a-z]*/)*([0-9]{6,11})[?]?.*
希望这会有所帮助......
答案 1 :(得分:28)
我认为使用parse_url()
是最佳选择:
$vimeo = 'https://vimeo.com/29474908';
echo (int) substr(parse_url($vimeo, PHP_URL_PATH), 1);
答案 2 :(得分:22)
对于那些想要看到使用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
答案 3 :(得分:12)
[编辑] 您现在可以通过API执行此操作!
如果您通过"链接提供逗号分隔的Vimeo网址列表"搜索端点的参数(https://developer.vimeo.com/api/endpoints/videos#GET/videos)我们会将这些视频作为API响应返回。
e.g。
GET https://api.vimeo.com/videos?links=https://vimeo.com/74648232,https://vimeo.com/232323497
<强> [原文] 强>
Vimeo提供了许多不同类型的视频网址,其中一些不包含ID。为了确保所有Vimeo网址的支持,您应该直接向vimeo索取ID。
您可以通过oEmbed endpoint询问vimeo。
有很多选项,但最简单的选择是向网址https://vimeo.com/api/oembed.json?url={vimeo_url}
发出HTTP GET请求,将{vimeo_url}
替换为相应的网址。
例如,要获取您在上面提供的网址的ID(https://vimeo.com/29474908),请向
发出HTTP GET请求https://vimeo.com/api/oembed.json?url=https://vimeo.com/29474908
解析JSON响应,并获取video_id
参数。
答案 4 :(得分:3)
这应该从各种vimeo网址中检索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];
答案 5 :(得分:2)
当前正在运行的正则表达式:
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.
&#13;
答案 6 :(得分:1)
如果有人在基于@ 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];
}
}
答案 7 :(得分:0)
如果仅需要Vimeo ID,则可以使用RegExp非捕获组:
(?:https?:\/\/)?(?:www\.)?vimeo\.com\/(?:(?:[a-z0-9]*\/)*\/?)?([0-9]+)
答案 8 :(得分: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 原始示例代码。