Youtube API - 提取视频 ID

2022-01-06 00:00:00 youtube php

我正在编写允许用户输入 Youtube 视频 URL 的功能.我想从这些网址中提取视频 ID.

I am coding a functionality that allows users to enter a Youtube video URL. I would like to extract the video ID from these urls.

Youtube API 是否支持我传递链接并返回视频 ID 的某种功能.还是我必须自己解析字符串?

Does Youtube API support some kind of function where I pass the link and it gives the video ID in return. Or do I have to parse the string myself?

我正在使用 PHP ......我很感激这方面的任何指针/代码示例.

I am using PHP ... I would appreciate any pointers / code samples in this regard.

谢谢

推荐答案

这是一个使用正则表达式从 URL 中提取 youtube ID 的示例函数:

Here is an example function that uses a regular expression to extract the youtube ID from a URL:

/**
 * get youtube video ID from URL
 *
 * @param string $url
 * @return string Youtube video id or FALSE if none found. 
 */
function youtube_id_from_url($url) {
    $pattern = 
        '%^# Match any youtube URL
        (?:https?://)?  # Optional scheme. Either http or https
        (?:www.)?      # Optional www subdomain
        (?:             # Group host alternatives
          youtu.be/    # Either youtu.be,
        | youtube.com  # or youtube.com
          (?:           # Group path alternatives
            /embed/     # Either /embed/
          | /v/         # or /v/
          | /watch?v=  # or /watch?v=
          )             # End path alternatives.
        )               # End host alternatives.
        ([w-]{10,12})  # Allow 10-12 for 11 char youtube id.
        $%x'
        ;
    $result = preg_match($pattern, $url, $matches);
    if ($result) {
        return $matches[1];
    }
    return false;
}

echo youtube_id_from_url('http://youtu.be/NLqAF9hrVbY'); # NLqAF9hrVbY

采用来自类似的问题.

这不是您正在寻找的直接 API,但可能有帮助.Youtube 有一个 oembed 服务:

It's not directly the API you're looking for but probably helpful. Youtube has an oembed service:

$url = 'http://youtu.be/NLqAF9hrVbY';
var_dump(json_decode(file_get_contents(sprintf('http://www.youtube.com/oembed?url=%s&format=json', urlencode($url)))));

它提供了更多关于 URL 的元信息:

Which provides some more meta-information about the URL:

object(stdClass)#1 (13) {
  ["provider_url"]=>
  string(23) "http://www.youtube.com/"
  ["title"]=>
  string(63) "Hang Gliding: 3 Flights in 8 Days at Northside Point of the Mtn"
  ["html"]=>
  string(411) "<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/NLqAF9hrVbY?version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/NLqAF9hrVbY?version=3" type="application/x-shockwave-flash" width="425" height="344" allowscriptaccess="always" allowfullscreen="true"></embed></object>"
  ["author_name"]=>
  string(11) "widgewunner"
  ["height"]=>
  int(344)
  ["thumbnail_width"]=>
  int(480)
  ["width"]=>
  int(425)
  ["version"]=>
  string(3) "1.0"
  ["author_url"]=>
  string(39) "http://www.youtube.com/user/widgewunner"
  ["provider_name"]=>
  string(7) "YouTube"
  ["thumbnail_url"]=>
  string(48) "http://i3.ytimg.com/vi/NLqAF9hrVbY/hqdefault.jpg"
  ["type"]=>
  string(5) "video"
  ["thumbnail_height"]=>
  int(360)
}

但 ID 不是响应的直接部分.但是,它可能包含您要查找的信息,并且可能有助于验证 youtube 网址.

But the ID is not a direct part of the response. However it might contain the information you're looking for and it might be useful to validate the youtube URL.

相关文章