How to get title, meta-description and meta-keywords of a webpage from url

  1. Previous
  2. Next
  3. Recent

    1. Signs 'He' May be Cheating on You
    2. 7 Vows of Hindu Marriage - Know What 'Saat Vachans' Mean
    3. RuPay vs Visa vs Mastercard vs American Express- A Comparison
    4. What is RuPay? Everything You Need to Know About India's Domestic Payment System!
    5. Top 10 Online Portals to Find a High Paying Job in India
    6. Top Indians who Revolutionized the IT Sector in India
    7. 8 Initiatives by Narendra Modi that Could Change the Future of the Nation
    8. 8 Work-from-home Jobs for Housewives, Students, and Part timers
    9. Complete Company Registration Process in India - Explained
    10. Change Name in India in 5 Steps - Filing Affidavit, Newspaper Ad & Gazette Notification

When you post a link as Facebook update, the title and meta-description of the webpage is automatically retrieved and displayed. Here is an example,

get title and get meta description of a webpage from url

The same thing happens in Digg when you submit a weblink.

So if you’re building a web application where you need to retrieve title, meta-description, meta-keywords or any other meta information of a webpage then read the following code carefully and the explanation below.

PHP code to get title, meta-description and meta-keywords


<?php

$submitted="http://yourravi.com/get-title-meta-description-meta-keywords-webpage-from-url-weblink-php-code";

$urlContents = file_get_contents($submitted);
$dom = new DOMDocument();
@$dom->loadHTML($urlContents);
$titly = $dom->getElementsByTagName('title');
$title=$titly->item(0)->nodeValue;

$metatagarray = get_meta_tags($submitted);
$keywords = $metatagarray["keywords"];
$description = $metatagarray["description"];

echo "<strong>URL: </strong >$submitted <br/>";
echo "<strong>Title:</strong> $title <br/>";
echo "<strong>Description: </strong >$description <br/>";
echo "<strong>Keywords: </strong >$keywords <br/>";

?>

Code Explaination

  1. In the above written php code, we’re retrieving title, description and keywords for the link ‘http://yourravi.com/get-title-meta-description-meta-keywords-webpage-from-url-weblink-php-code’. You can use any url here. Optionally, you can also put a input box and ask user to provide a weblink. Then catch the entered url and display the title and meta information for it. This is exactly what happens in Facebook, Digg and other Web 2.0 sites.
  2. Next we retrieve the HTML code of the submitted url/weblink, and extract the title from it. For doing this, we use the DOMDocument php class and its few functions.
  3. Getting meta-description, keywords, author and meta information is very easy. For this we just have to use the ‘get_meta_tags()‘ function of php.
  4. Here we’re actually loading the entire HTML code, so displaying results would take some time [2-3 seconds].

[If there is any better/easy solution ,or any query, please mention in comments.]

blog comments powered by Disqus