程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了YouTube API-提取视频ID大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决YouTube API-提取视频ID?

开发过程中遇到YouTube API-提取视频ID的问题如何解决?下面主要结合日常开发的经验,给出你关于YouTube API-提取视频ID的解决方法建议,希望对你解决YouTube API-提取视频ID有所启发或帮助;

以下是使用正则表达式从URL提取youtube ID的示例函数:

/**
 * 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 scheR_734_11845@e. 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有一项固定服务:

$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的更多元信息:

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 URL有用。

解决方法

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

Youtube API是否支持某种函数,我可以通过该函数传递链接,它会返回视频ID。还是我必须自己解析字符串?

我正在使用php &Hellip;在这方面,我将不胜感激任何指针/代码示例。

谢谢

大佬总结

以上是大佬教程为你收集整理的YouTube API-提取视频ID全部内容,希望文章能够帮你解决YouTube API-提取视频ID所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。