Programming

PHP Method To Fetch all Metatags from URL

Written by     on    02 Jan, 2020     0      

Do you want to fetch meta tags from other websites? You are at the right place as I am a developer and after analysis, I have created the code that helps you to fetch all the Metatags from the URL.

This is the way it should be :

<pre>
<xmp>
<?php

//Enter your url with http:// OR https://
$site_url = "https://www.localseowork.com"; //please enter http:// after checke


//Default VAR null
$exists_url = 0;
$title = "";
$description = "";
$og_description = "";
$author = "";
$keywords = "";
$og_image = "";
$twitter_image = "";


//site_url checke to website url exit 
$file_headers = @get_headers($site_url);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists_url = 0; //not exit
}
else {
    $exists_url = 1; //website exit
}


//Get webpage title
function get_site_title($url) {
    $match = [0];
    $page = file_get_contents($url);
    $title = preg_match('/<title[^>]*>(.*?)<\/title>/ims', $page, $match) ? $match[1] : null;
    return $title;
}


    //Checke exit url start
    if($exists_url == 1) {

        $html = new DOMDocument();
        @$html->loadHTML(file_get_contents($site_url));

        //get all meta tags loop start
        foreach($html->getElementsByTagName('meta') as $meta) {
            
            if ($meta->hasAttribute('property') && strpos($meta->getAttribute('property'), 'og:') === 0) {
                if($meta->getAttribute('property')=='og:image'){ 
                    $og_image = $meta->getAttribute('content');
                }
                if($meta->getAttribute('property')=='og:description'){
                    $og_description = $meta->getAttribute('content');
                }      
            }
            
            if($meta->getAttribute('name') == 'twitter:image:src'){
                $twitter_image = $meta->getAttribute('content');
            }

            /*
                --- You can get more metadata to start hear ---
                $meta->getAttribute('YOUR_META_ATTR_NAME') == 'YOUR_META_ATTR_NAME_VALUE'
            */

        }
        //get all meta tags loop end



        //get keyword & description
        $meta_tags_defualt = get_meta_tags($site_url);
        if(isset($meta_tags_defualt['keywords'])){
            $keywords = $meta_tags_defualt['keywords'];
        }
        if(isset($meta_tags_defualt['description'])){
            $description = $meta_tags_defualt['description'];
        }
        if(isset($meta_tags_defualt['author'])){
            $author = $meta_tags_defualt['author'];
        }
        


        //get web site title 
        $title = get_site_title($site_url);       

    }
    //Checke exit url end



    //Output Hear
    if($exists_url == 1){
        echo "<b>Title : </b>".$title;
        echo "<br><br>";
        echo "<b>Description : </b>".$description;
        echo "<br><br>";
        echo "<b>OG Description : </b>".$og_description;
        echo "<br><br>";
        echo "<b>Author : </b>".$author;
        echo "<br><br>";
        echo "<b>Keywords : </b>".$keywords;
        echo "<br><br>";
        echo "<b>OG Image : </b>".$og_image;
        echo "<br><br>";
        echo "<b>Twitter Image : </b>".$twitter_image;
    } else {
        echo "Please enter a valid url to analyzer";
    }

?>


</xmp>
</pre>

In Case if you have missed any of social media meta tags then Copy Below Code and Paste before section of website Pages

<pre>
<xmp>            
Note: Replace your own content under the content="Paste content here"

<!-- HTML Meta Tags -->
<title>your_site_title</title>
<meta name="description" content="your_site_description">

<!-- Facebook Meta Tag -->
<meta property="og:url" content="your_site_url">
<meta property="og:type" content="website"> //no change
<meta property="og:title" content="your_site_title">
<meta property="og:description" content="your_site_description">
<meta property="og:image" content="your_site_image">

<!-- Twitter Meta Tag -->
<meta name="twitter:card" content="summary_large_image"> //no change
<meta name="twitter:title" content="your_site_title">
<meta name="twitter:description" content="your_site_description">
<meta name="twitter:image" content="your_site_image">

<!-- Google / Search Engine Tag -->
<meta itemprop="name" content="your_site_title">
<meta itemprop="description" content="your_site_description">
<meta itemprop="image" content="your_site_image">

</xmp>
</pre>


Check Out Live demo of this code, How it works : toolfear.com

I am Sure this PHP and javascript code helps to fetch metatags, incase if you have trouble to setup you can ask me under the below comment section.

Tags :  check meta tags onlineget meta tags from urlget meta tags from url javascripthey meta tagsmeta description generator from urlmeta tag generator for bloggerMeta Tags Analyzer - OnlinePHP get all meta tagsphp get meta tags propertyPHP meta tagssocial media card previewsocial meta tags generator

Write a Reply or Comment

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

Submit Guest Post Content
Are you a passionate writer or expert in our categories? You can submit content on our website. Before writing content, you should read our guest posting guidelines.
Read More
NEVER MISS A POST
Get the latest posts and awesome deals delivered to your inbox for free. 100% Privacy.