【问题标题】:Extract YouTube Image From String从字符串中提取 YouTube 图像
【发布时间】:2015-02-03 11:46:07
【问题描述】:

所以我的字符串是

<div class="cm-video">
<iframe width="100%" height="100%" src="http://www.youtube.com/embed/ATYklyGZU4I?feature=oembed" frameborder="0" allowfullscreen></iframe>
</div>

我已阅读How do I get a YouTube video thumbnail from the YouTube API?,但不确定如何专门检索图像

【问题讨论】:

    标签: php regex iframe youtube


    【解决方案1】:

    $videoURL = "https://www.youtube.com/watch?v=MVQOmonaFOg";

    $videoAPI = "https://www.youtube.com/oembed?format=json&url=";

    $data = json_decode(file_get_contents($videoAPI . urlencode($videoURL)));

    print_r($data);

    【讨论】:

      【解决方案2】:

      试试这个代码,它会给你视频图像的网址。

      $url = "http://www.youtube.com/embed/ATYklyGZU4I?feature=oembed";
      echo getYouTubeVideoImage($url);
      
      function getYouTubeVideoImage($youtube_code){
          preg_match('/youtube\.com\/v\/([\w\-]+)/', $youtube_code, $match);
      
      
          if(!isset($match[1]) || $match[1] == ''){
              preg_match('/youtube\.com\/embed\/([\w\-]+)/', $youtube_code, $match);
          }
          if(!isset($match[1]) || $match[1] == ''){
              preg_match('/v\=(.+)&/',$youtube_code ,$match);
          }
      
          $full_size_thumbnail_image = "http://img.youtube.com/vi/".$match[1]."/0.jpg";
          $small_thumbnail_image1 = "http://img.youtube.com/vi/".$match[1]."/1.jpg";
          $small_thumbnail_image2 = "http://img.youtube.com/vi/".$match[1]."/2.jpg";
          $small_thumbnail_image3 = "http://img.youtube.com/vi/".$match[1]."/3.jpg";
      
          // return whichever thumbnail image you would like to retrieve
          return $full_size_thumbnail_image;      
      }
      

      【讨论】:

        【解决方案3】:

        只需调用具有所需格式的&lt;img&gt; 元素,例如 php:

        $id = "ATYklyGZU4I";
        echo "<img src=http://img.youtube.com/vi/$id/1.jpg>";
        

        您的链接中提到了其他格式。

        【讨论】:

          猜你喜欢
          • 2021-01-01
          • 2013-02-03
          • 2016-07-21
          • 2012-09-22
          • 2014-03-10
          • 2012-09-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多