php

How to extract meta tags from multiple websites?


I am using this code to extract meta tags for one website but I am trying to get meta tags of multiple websites using a file.

<?php
$tags = get_meta_tags('https://demotiger.com');
$variable = $tags['description']; 
echo $variable;
?>

I tried to modify this code to get it to work for multiple websites:

<?php
$file = file_get_contents('website-list.txt');
$tags = get_meta_tags('$file');
$variable = $tags['description']; 
echo $variable;
?>

wesbite-list.txt

https://example.com https://example2.com

Error I am getting:

Warning: get_meta_tags($file): failed to open stream: No such file or directory in C:\someserver\www\index.php on line 3


Solution

  • 1.Since your wesbite-list.txt contains multiple lines, so you need to extract this file contents as an array. For that use file()

    2.After that iterate over array and get the tags.

    <?php
    
    $websites = file('website-list.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach($websites as $website){
        $tags = get_meta_tags($website);
        echo $tags['description'];
        echo PHP_EOL;
    }