How can I retrieve YouTube video details from video URL using PHP?

How can I retrieve YouTube video details from video URL using PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Parsing YouTube Video Metadata in PHP

Retrieving metadata from a YouTube video URL—specifically the title, description, and thumbnail—requires moving beyond simple string manipulation. The data does not reside in a publicly accessible JSON endpoint that accepts just the video ID; instead, it is embedded within an HTML page rendered by the browser. To extract this information via PHP, you must interact with the YouTube Data API v3 or parse the response from the standard watch page using a library like DOMDocument.

The most robust approach for production environments involves utilizing the YouTube Data API v3. This method returns structured JSON data directly from Google's servers, ensuring consistency regardless of how YouTube changes their frontend HTML structure. However, this approach requires registering an application with Google Cloud Console to obtain an API key and setting appropriate quotas. If you need a quick script without authentication overhead, parsing the DOM of the video page is a viable alternative, though it carries the risk of breaking if YouTube alters its template code.

When using the API v3, the endpoint videos.list is the standard entry point. You must construct a request that includes your API key as a query parameter and specifies the fields you wish to retrieve. For instance, to get the title, description, and thumbnail URL for a video with ID B4CRkpBGQzU, the request would look like this:

GET https://www.googleapis.com/youtube/v3/videos?id=B4CRkpBGQzU&part=snippet,statistics&key=YOUR_API_KEY

The response object contains nested arrays. The title and description are located within the snippet field under title and description, respectively. The high-resolution thumbnail is available at snippet.thumbnails.high.url. If you do not have an API key or cannot implement OAuth2 for your project, you can bypass authentication by fetching the HTML page directly using PHP's cURL extension and parsing the resulting document.

$url = 'https://www.youtube.com/watch?v=B4CRkpBGQzU';
$ch = curl_init($url);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
]);
$html = curl_exec($ch);
curl_close($ch);

// Load the HTML into a DOMDocument
$dom = new DOMDocument();
libxml_use_internal_errors(true); // Suppress warnings from malformed HTML
$dom->loadHTML($html);

// Extract title
$titleNode = $dom->getElementsByTagName('meta')->item(0);
$title = $titleNode->getAttribute('content');

// Extract description (often a data-text attribute or meta tag)
// Note: YouTube frequently changes these selectors. This is brittle.

Parsing the HTML directly introduces significant fragility. YouTube updates its DOM structure regularly; a selector that works today might fail next month. Additionally, this method cannot reliably fetch the full description if it exceeds character limits on the page, nor does it easily provide structured statistics like view counts without complex regex matching. The API approach remains superior for applications requiring reliability.

If you are building a larger application where managing API keys and quotas adds unnecessary complexity to your setup, consider using a dedicated package that abstracts these concerns. Tools available on https://laravelcompany.com often provide pre-configured services or packages that handle the boilerplate of API authentication and rate limiting, allowing you to focus on data processing rather than networking logic.

Regardless of the method chosen, always respect rate limits. The free tier of the YouTube Data API v3 offers 10,000 units per day, where a simple list request consumes roughly 1 unit. Exceeding this limit will result in an error response unless you implement exponential backoff strategies or upgrade your plan. When parsing HTML, be mindful that libxml_use_internal_errors is essential because YouTube's response often contains malformed SVG or JavaScript within the page source that standard PHP XML parsers will otherwise choke on.

For most modern applications, leveraging the official API ensures you get accurate data and avoids the maintenance burden of updating your parser when YouTube's frontend evolves.

Our network