In this article, I am going to show you how to get webpage Titles and Meta tags from external website URLs using PHP. Scroll down to the page so you can see the full source code available for this.

Mostly, All external websites are used to specify the 3 common metadata for the web page are page title, page description, and page keywords. These all meta tags information fetched using PHP. The page description and page keywords within tag and page title is <title> and </title> tag.

codeat21
<?php // Web page URL $url = 'https://codeat21.com/'; // Extract HTML using curl $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $data = curl_exec($ch); curl_close($ch); // Load HTML to DOM object $dom = new DOMDocument(); @$dom->loadHTML($data); // Parse DOM to get Title data $nodes = $dom->getElementsByTagName('title'); $title = $nodes->item(0)->nodeValue; // Parse DOM to get meta data $metas = $dom->getElementsByTagName('meta'); $description = ''; $keywords = ''; $site_name = ''; $image = ''; for($i=0; $i<$metas->length; $i++){ $meta = $metas->item($i); if($meta->getAttribute('name') == 'description'){ $description = $meta->getAttribute('content'); } if($meta->getAttribute('name') == 'keywords'){ $keywords = $meta->getAttribute('content'); } if($meta->getAttribute('property') == 'og:site_name'){ $site_name = $meta->getAttribute('content'); } if($meta->getAttribute('property') == 'og:image'){ $image = $meta->getAttribute('content'); } } echo "Title: $title". '<br/><br/>'; echo "Description: $description". '<br/><br/>'; echo "Keywords: $keywords". '<br/><br/>'; echo "site_name: $site_name". '<br/><br/>'; echo "image: $image"; ?>

Output

  • Title: Home - codeat21.com - Web Designing and Development Tutorials Website.
  • Description: codeat21.com is a web designing and development tutorials website. Learn Web Development, HTML, CSS, PHP, MySQL, JavaScript, Node JS, React JS, jQuery, etc..
  • Keywords:
  • site_name: codeat21.com - Web Designing and Development Tutorials Website.
  • image:

related keywords

  • Get meta tags from URL
  • Get meta tags from url javascript
  • Get_meta_tags
  • how to add meta tags in php website
  • How to add meta tags dynamically in PHP
  • Php get title from url
  • How to add meta title and description in php
  • php get page title from url, curl get meta tags
  • get og meta tags javascript
  • dynamically change og meta tags