Extracting Title, Meta keywords and Meta Description using PHP

by guruprasad 2014-07-05 10:31:19

To Extract the title and meta keywords and description in php:
 
function title_extract($url)
{
    $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);

    return $data;
}

$html = title_extract("url where requirements to be fetched");

$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');

$title = $nodes->item(0)->nodeValue;

$meta_details = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $meta_details->length; $i++)
{
    $meta = $meta_details->item($i);
    if($meta->getAttribute('name') == 'description')
        $meta_description = $meta->getAttribute('content');
    if($meta->getAttribute('name') == 'keywords')
        $meta_keywords = $meta->getAttribute('content');
}

echo "<table border='1'><tr><th>Title</th><th>Keywords</th><th>Description</th></tr>";
echo "<tr><td>$title</td>";
echo "<td>$meta_description</td>";
echo "<td>$meta_keywords</td></tr></table>"; 

Tagged in:

667
like
0
dislike
0
mail
flag

You must LOGIN to add comments