Php get youtube thumbnails

Show Image Thumbnail by YouTube Video URL using PHP

Thumbnails are used for preview a single or group of media entities in a single explorer window. By showing thumbnails, we can have a shortcut or quick-view to see more voluminous data without navigation.

Websites use thumbnails to showcase content in a gallery format. For example, the online shopping cart websites showcase their product gallery with image thumbnails. The news broadcasting websites are showing video thumbnails for a quick look into the videos.

I created the thumbnail image URL from the YouTube video source URL using PHP. The video URL is given as an input posted via HTML form.

In PHP, the unique id of the video is extracted and used to form the image thumbnail source. If you want to show an image thumbnail in a gallery for your favorite YouTube videos, you can use this example to get the thumbnail source URL.

This screenshot shows the thumbnail image output created by using the given YouTube URL.

image-thumbnail-output-for-youtube-video

Getting Video URL using HTML Form

This HTML code is used to show a HTML form with an input option to let the user enter the YouTube video URL. After submitting the form, the input URL is processed in a PHP file to show the image thumbnail.

Show Image Thumbnail by YouTube Video URL using PHP



Thumbnail Image:

?>

Create Thumbnail Source From Video using PHP Code

This PHP code reads the video URL posted via the HTML form. It extracts the unique id from the video using PHP explode(). Then it creates the thumbnail source URL by using the unique id of the video.

This source URL is used to show the preview of the video using thumbnail image.

Leave a Reply Cancel reply

  • PHP
    • Learn PHP
    • PHP Introduction
    • PHP Basics
    • PHP Shopping Cart
    • Payment Gateway
    • Contact Form
    • PHP Login Registration
    • Comments System
    • Types, Variables & Operators
    • PHP Strings
    • PHP Arrays
    • PHP Functions
    • PHP OOPS
    • Event Management System
    • PHP Mail
    • PHP Forms
    • Advanced
    • PHP AJAX
    • RESTful API
    • PHP Databases
    • PHP Sessions and Cookies
    • Error and Exception Handling
    • File Upload
    • File Import Export
    • Files and Directories
    • PHP Date Time
    • PHP XML
    • PHP CSV
    • PHP JSON
    • PHP Code Samples
      • PHP MySQL Date Range Search with jQuery DatePicker
      • Advanced Search using PHP
      • PHP CRUD with Search and Pagination
      • Highlighting Keywords in Search Results with PHP
      • PHP Pagination MySQL Database Example Script with Previous Next like Google
      • Command Line PHP
      • How to Create Custom Image File Explorer in PHP
      • Web Scraping with PHP — Tutorial to Scrape Web Pages
      • User Login Session Timeout Logout in PHP
      • PHP Change Password Script
      • Object-Oriented Poll System using PHP
      • PHP cURL
      • Extract Content using PHP and Preview like Facebook
      • Show Image Thumbnail by YouTube Video URL using PHP
      • Star Rating with PHP and jQuery AJAX
      • How to Crop Image using jQuery and PHP
      • How to Generate Initial Avatar Image from Username using PHP Dynamically
      • Live search in PHP and MySQL with AJAX

      I’m currently available for freelance work.

      1. Simple PHP Shopping Cart
      2. Stripe Payment Gateway Integration using PHP
      3. User Registration in PHP with Login: Form with MySQL and Code Download
      4. PHP Contact Form
      5. How to Create Dynamic Stacked Bar, Doughnut and Pie charts in PHP with Chart.js

      “ The whole experience was extremely professional. We agreed on the requirements and scope of work quickly. The price was fair. Vincy promised quick turn-around and delivered on time . ”

      Michael Goldstein, HHG Development Associates LLC, USA

      Источник

      How do I get a YouTube video thumbnail from the YouTube API?

      To get a thumbnail image for a YouTube video in PHP, you can use the file_get_contents function to make a GET request to the videos.list method of the YouTube Data API.

      Here’s an example of how to do this:

       // Replace YOUR_API_KEY with your actual API key $apiKey = 'YOUR_API_KEY'; // Replace VIDEO_ID with the ID of the video for which you want to get the thumbnail $videoId = 'VIDEO_ID'; // Set the API endpoint for the videos.list method $apiEndpoint = 'https://www.googleapis.com/youtube/v3/videos'; // Set the parameters for the API request $params = [ 'id' => $videoId, 'key' => $apiKey, 'part' => 'snippet,contentDetails', ]; // Build the query string $query = http_build_query($params); // Make the GET request to the API endpoint $response = file_get_contents($apiEndpoint . '?' . $query); // Decode the JSON response $responseData = json_decode($response, true); // Get the thumbnail image URL from the response data $thumbnailUrl = $responseData['items'][0]['snippet']['thumbnails']['default']['url']; // Use the thumbnail image URL to display the thumbnail image echo ' Thumbnail'; ?>

      This code makes a GET request to the videos.list method of the YouTube Data API, passing in the API key, the video ID, and the part parameter set to snippet,contentDetails . The API returns a JSON object containing data about the video, including the thumbnail image URL in the snippet.thumbnails.default.url field. The code then decodes the JSON response and extracts the thumbnail image URL, which it uses to display the thumbnail image.

      Источник

      Get YouTube Video Title, Description & Thumbnail from URL in PHP

      Hi! In this tutorial I’m going to show you how to get youtube video details like title, description, thumbnail image etc from a video url using php script. With the help of Google’s YouTube Data API you can fetch information about youtube videos. In general each youtube video will have a specific ‘ID’ associated to it. To retrieve data about a video you have to pass this video id while making the api call. And the api in turn returns back the video data as json response.

      YouTube API is not only helpful to fetch basic video info but also let you create playlists, channels, implement youtube search and much more.

      Get YouTube Data API Key

      YouTube Data API is free to use but first you must get access to it. You need a Google account to get api access and is provided in the form of an api key.

      1. To get api-key, go to https://console.developers.google.com and login with your Google account.

      2. Once you are in, create a new project and click on ‘ENABLE API’ link at the top.

      3. Now scroll down to ‘Youtube APIs’ section and select ‘Youtube Data API’.

      get youtube data api key 1

      4. Then click on the ‘ENABLE’ link at the top-right side of the page to enable youtube data api for your account.

      get youtube data api key 2

      5. Once enabled, select ‘Credentials’ on the left-pane. Under credentials click on ‘Create credentials’ button and choose ‘API key’ in the dropdown.

      get youtube data api key 3

      7. Your api key will be generated and shown in a popup. Save the key to a text file for later use.

      get youtube data api key 4

      Done! Now we have the key to access youtube api. Let’s move on to the coding part.

      Fetching YouTube Video Information

      In general YouTube videos share a common url structure and they look like this.

      Where ‘VIDEO_ID’ represents the individual video id.

      You have to take this video id from the youtube video url and use it to fetch details.

      Like I already said, to fetch video information you have to call the youtube api. And you must also send the corresponding ‘VIDEO_ID’ and ‘parts’ of the information you need to retrieve. Following is the php script to do it.

      PHP Script to Get YouTube Video Details

      The above snippet will produce an output like this.

      Array ( [kind] => youtube#videoListResponse [etag] => "m2yskBQFythfE4irbTIeOgYYfBU/YJEU8MCit4Jt-7o2NPm81zgElAQ" [pageInfo] => Array ( [totalResults] => 1 [resultsPerPage] => 1 ) [items] => Array ( [0] => Array ( [kind] => youtube#video [etag] => "m2yskBQFythfE4irbTIeOgYYfBU/Sx66nqOT7ouBnClI9ImzyEA1n90" [id] => H4Jx4oefSjw [snippet] => Array ( [publishedAt] => 2013-08-01T04:12:47.000Z [channelId] => UCj4LfrxdH7TnUZlyHOKgL5A [title] => 01. Programming in Objective-C - Introduction [description] => Ever wondered what Objective-C actually is? Historically, it was the first attempt to make C objectified. C++ came later. NextStep, and now OSX, popularized this very powerful language. So sit back, as Yari D`Areglia takes you through the first steps in getting started with this language. [thumbnails] => Array ( [default] => Array ( [url] => https://i.ytimg.com/vi/H4Jx4oefSjw/default.jpg [width] => 120 [height] => 90 ) [medium] => Array ( [url] => https://i.ytimg.com/vi/H4Jx4oefSjw/mqdefault.jpg [width] => 320 [height] => 180 ) [high] => Array ( [url] => https://i.ytimg.com/vi/H4Jx4oefSjw/hqdefault.jpg [width] => 480 [height] => 360 ) [standard] => Array ( [url] => https://i.ytimg.com/vi/H4Jx4oefSjw/sddefault.jpg [width] => 640 [height] => 480 ) [maxres] => Array ( [url] => https://i.ytimg.com/vi/H4Jx4oefSjw/maxresdefault.jpg [width] => 1280 [height] => 720 ) ) [channelTitle] => Dilan Damith Prasanga I.G.  => Array ( [0] => Objective-C (Programming Language) [1] => Programming In Objective-C [2] => Programming Language (Literary Genre) ) [categoryId] => 27 [liveBroadcastContent] => none [localized] => Array ( [title] => 01. Programming in Objective-C - Introduction [description] => Ever wondered what Objective-C actually is? Historically, it was the first attempt to make C objectified. C++ came later. NextStep, and now OSX, popularized this very powerful language. So sit back, as Yari D`Areglia takes you through the first steps in getting started with this language. ) ) ) ) )

      As you can see, we have decoded the json response to php array and it contains details like video title, description, thumbnail urls, view count, channel id and much more. Now you must parse this array to extract individual data like title, description etc.

      To Get YouTube Video Title,

      To Get YouTube Video Description,

      To Get YouTube Video Thumbnail URL,

      YouTube stores four different types of thumbnails at various resolutions for a single video. Above, I have taken the default thumbnail image url.

      Once you’ve got the video info, use it as per your liking. Here is a simple example in which I have displayed video details in a web page.

      Example

      Title: ' . $data['items'][0]['snippet']['title'] . ''; echo ''; echo '

      ' . $data['items'][0]['snippet']['description'] . '

      '; ?>

      fetch youtube video title description and thumbnail in php

      I hope now you have clear understanding of using youtube data api to fetch youtube video details using php. This is just a taste of what youtube api can do. Obviously there’s more to it. I’ll cover the api more in detail in future posts. If you like this tutorial, don’t forget to share it in social media.

      Источник

      Get YouTube Video Thumbnail Image URL using PHP

      get-youtube-thumbnail-image-url-using-php-codexworld

      In the present day, YouTube embed video is used on many websites. Many web project needs to implement a YouTube video gallery where YouTube video thumbnails are dynamically listed in the gallery. In this article, we’ll provide the short PHP snippet to get YouTube video thumbnail image URL using PHP.

      This simple PHP script is very useful when you want to get the YouTube video thumbnail image URL dynamically based on the provided YouTube URL. Using this YouTube thumbnail URL, you can design the video gallery as per your project needs.

      Get Thumbnail Image URL from YouTube Embed Code

      The following PHP code extracts the src value from YouTube Embed Code and generates the video thumbnail image URL based on YouTube video ID.

      $embedCode = '
      '
      ;
      preg_match('/src="https://www.codexworld.com/youtube-video-thumbnail-image-url-php/([%5E"]+)"/', $embedCode, $match);

      // Extract video url from embed code
      $videoURL = $match[1];
      $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 '.$thumbURL.'"/>';

      Get Thumbnail Image URL from YouTube URL

      The following PHP code generates the video thumbnail image URL based on YouTube video ID.

      // YouTube video url
      $videoURL = 'https://youtu.be/dwJasig9Olw';
      $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 '.$thumbURL.'"/>';

      YouTube Video Thumbnail Image Sizes

      YouTube allow you to get the following thumbnail sizes.

      http://img.youtube.com/vi/VideoID/0.jpg http://img.youtube.com/vi/VideoID/1.jpg http://img.youtube.com/vi/VideoID/2.jpg http://img.youtube.com/vi/VideoID/3.jpg

      Also the different quality of thumbnails is available.

      http://img.youtube.com/vi/VideoID/default.jpg http://img.youtube.com/vi/VideoID/hqdefault.jpg http://img.youtube.com/vi/VideoID/mqdefault.jpg http://img.youtube.com/vi/VideoID/sddefault.jpg

      Are you want to get implementation help, or modify or enhance the functionality of this script? Click Here to Submit Service Request

      If you have any questions about this script, submit it to our QA community — Ask Question

      Источник

      Читайте также:  Php check that array is empty
Оцените статью