Search the blog

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.

Set the URL of the Web Page

$url = "http://www.domain.com/";

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.

Load in the File

$fp          = fopen($url,'r');
$content     = "";

while(!feof($fp)) {

    $buffer   = trim(fgets($fp, 4096));
    $content .= $buffer;

}

Then using some we can get the contents of the title tag.

Get Contents of the Title Tag

$start   = '';
$end     = '<\/title>';

preg_match("/$start(.*)$end/s", $content, $match);

$title = $match[1];

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.

Get Meta Tags

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

Then all that remains is to output the details (use the variables how you like in your web project):

Output Details

<?php

$url = "http://www.drquincy.com/";
$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);
$keywords = $metatagarray["keywords"];
$description = $metatagarray["description"];

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

?>
Tim Bennett is a Leeds-based web designer from Yorkshire. He has a First Class Honours degree in Computing from Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.