• <bdo id='9PweY'></bdo><ul id='9PweY'></ul>
    <i id='9PweY'><tr id='9PweY'><dt id='9PweY'><q id='9PweY'><span id='9PweY'><b id='9PweY'><form id='9PweY'><ins id='9PweY'></ins><ul id='9PweY'></ul><sub id='9PweY'></sub></form><legend id='9PweY'></legend><bdo id='9PweY'><pre id='9PweY'><center id='9PweY'></center></pre></bdo></b><th id='9PweY'></th></span></q></dt></tr></i><div id='9PweY'><tfoot id='9PweY'></tfoot><dl id='9PweY'><fieldset id='9PweY'></fieldset></dl></div>

        <small id='9PweY'></small><noframes id='9PweY'>

      1. <tfoot id='9PweY'></tfoot>

      2. <legend id='9PweY'><style id='9PweY'><dir id='9PweY'><q id='9PweY'></q></dir></style></legend>

      3. Youtube API - 提取视频 ID

        时间:2023-10-11

        <tfoot id='rItrE'></tfoot>
        <i id='rItrE'><tr id='rItrE'><dt id='rItrE'><q id='rItrE'><span id='rItrE'><b id='rItrE'><form id='rItrE'><ins id='rItrE'></ins><ul id='rItrE'></ul><sub id='rItrE'></sub></form><legend id='rItrE'></legend><bdo id='rItrE'><pre id='rItrE'><center id='rItrE'></center></pre></bdo></b><th id='rItrE'></th></span></q></dt></tr></i><div id='rItrE'><tfoot id='rItrE'></tfoot><dl id='rItrE'><fieldset id='rItrE'></fieldset></dl></div>
            <bdo id='rItrE'></bdo><ul id='rItrE'></ul>
            1. <small id='rItrE'></small><noframes id='rItrE'>

                <tbody id='rItrE'></tbody>

                <legend id='rItrE'><style id='rItrE'><dir id='rItrE'><q id='rItrE'></q></dir></style></legend>
                  本文介绍了Youtube API - 提取视频 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写允许用户输入 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.

                  这篇关于Youtube API - 提取视频 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Mysql/php 树形导航菜单 下一篇:从任何 YouTube URL 获取 YouTube 视频 ID 的 RegEx 模式

                  相关文章

                • <tfoot id='g14Ae'></tfoot>

                    <legend id='g14Ae'><style id='g14Ae'><dir id='g14Ae'><q id='g14Ae'></q></dir></style></legend>
                      <bdo id='g14Ae'></bdo><ul id='g14Ae'></ul>
                  1. <i id='g14Ae'><tr id='g14Ae'><dt id='g14Ae'><q id='g14Ae'><span id='g14Ae'><b id='g14Ae'><form id='g14Ae'><ins id='g14Ae'></ins><ul id='g14Ae'></ul><sub id='g14Ae'></sub></form><legend id='g14Ae'></legend><bdo id='g14Ae'><pre id='g14Ae'><center id='g14Ae'></center></pre></bdo></b><th id='g14Ae'></th></span></q></dt></tr></i><div id='g14Ae'><tfoot id='g14Ae'></tfoot><dl id='g14Ae'><fieldset id='g14Ae'></fieldset></dl></div>

                    <small id='g14Ae'></small><noframes id='g14Ae'>