Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

javascript - Get youtube video thumbnail

I'm trying to get the thumbnail image of a YouTube video from its url, which I get correctly with this script.

if ($resulf = $mysqli->query("SELECT * FROM vid")) {
                while ($rowcontentf = mysqli_fetch_array($resulf)){
                  ?>
                  <a class="angled-img" href="<?php echo $rowcontentf['link']; ?>">
                    <div class="img">
                      
                      <?php
                      // YouTube video url TEST
                      //$videoURL = 'https://www.youtube.com/watch?v=fRh_vgS2dFE';
                      $videoURL = '.$rowcontentfinal['link'].';  // YouTube video url SQL
                      $urlArr = explode("/",$videoURL);
                      $urlArrNum = count($urlArr);

                      // Youtube video ID
                      $youtubeVideoId = $urlArr[$urlArrNum - 1];

                      // Generate youtube thumbnail url
                      $thumbURL = 'http://img.youtube.com/vi/'.$youtubeVideoId.'/0.jpg';

                      // Display thumbnail image
                      echo '<img src="'.$thumbURL.'"/>';
                      ?>
                    </div>
                    <i class="fa fa-play icon"></i>
                  </a>
                  <?php
                }
              }

But dealing with a very basic example with Database, the URL I get from a SQL query. But I can't get the video thumbnail from the URL if I get the latter (URL) from my Database.

// YouTube video url TEST
//$videoURL = 'https://www.youtube.com/watch?v=fRh_vgS2dFE';

$videoURL = '.$rowcontentfinal['link'].';  // YouTube video url SQL
question from:https://stackoverflow.com/questions/65878965/get-youtube-video-thumbnail

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's because you're accidentally escaping your text by using the same quotes.

Try:

$videoURL = '.$rowcontentfinal["link"].';

For this reason, I'd recommend sticking to one quote style for strings and one quote style for "code". eg in he above singles for strings and doubles for "code"

Alternately you can try escaping the internal quotes

$videoURL = '.$rowcontentfinal['link'].';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...