Last updated on February 18th, 2022 at 07:41 pm

If you run any website that accepts submitted links, be it a tutorial website, a directory or a bookmarking site, you may find it useful to automatically retrieve information about a URL. Wouldn’t it be nice if you ran a web directory and you could bring up the title, keywords and description of a URL without the user having to enter it? This tutorial shows you how this can be achieved via PHP.

Firstly we need to set the URL of the web page. Note that it must contain the http prefix as we are going to load some data in through a HTTP file stream.
Code: Set the URL of the Web Page

$url = “http://www.mistonline.in”;

Now that’s done we need to get the contents of the title tag. We do this by open up a file stream using file() and reading the contents of the page in and storing it in a variable.
Code: Load in the File

$fp = fopen( $url, 'r' );
$content = "";
while( !feof( $fp ) ) {
$buffer = trim( fgets( $fp, 4096 ) );
$content .= $buffer;

The keywords and description of a web page are stored in the page’s meta tags. Fortunately, PHP has built-in functionality to get meta tag contents so it will be easier to do than the title tag. We call the get_meta_tags() function, which copies all meta tags into an associative array. We then copy the elements of the array we need to some variables.

Code: Get Meta Tags

$metatagarray = get_meta_tags($url);
$description = $metatagarray[ "description" ];
echo "<div><strong>URL:</strong> $url</div>\n";
echo "<div><strong>Title:</strong> $title</div>\n";
echo "<div><strong>Description:</strong> $description</div>\n";

Here’s the complete code listing.

<?php
$url = $_GET['url'];
if(!isset($url))
{
echo "Please enter URL starting with http:// and also have a <b>url</b> query string<p>For example: http://demo.mistonline.in/getmeta.php?<b>url=http://mistonline.in</b>";
exit();
}
$fp = fopen( $url, 'r' );
$content = "";
while( !feof( $fp ) ) {$buffer = trim( fgets( $fp, 4096 ) );
$content .= $buffer;
}
$start = '<title>';
$end = '<\/title>';
preg_match( "/$start(.*)$end/s", $content, $match );
$title = $match[ 1 ];
$metatagarray = get_meta_tags( $url );
$description = $metatagarray[ "description" ];
echo "<div><strong>URL:</strong> $url</div>\n";
echo "<div><strong>Title:</strong> $title</div>\n";
echo "<div><strong>Description:</strong> $description</div>\n";
?>

DEMO

One thought on “How to get web page title and meta tags using PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *