I know you are searching for PHP code to fetch meta tags from URLs. Well, today I am going to share with you the PHP code to fetch all the meta tags from the URL and select each meta tag.
PHP provide a predefined function for this purpose. So we can easily fetch meta tags from the URL. If you are aiming to develop a web application this tutorial is going to help you.
You need to create 2 files, index.php and style.css in a Folder named GetMeta. Index.php contains the basic HTML layout and PHP code to fetch meta tags. Also, style.css has CSS to make index.php attractive.
I created a basic HTML template from scratch. You can use bootstrap to make your work easier.
Remember: In sometimes the code can't able to fetch meta details, this is because the site that we are trying to fetch meta details is prohibited from accessing by other sites.
PHP function for fetch meta details
get_meta_tags() is the predefined function that enables you to fetch the mete details. The mete details are fetched in the form of an array.
Major doubts
- Is it working? It is 100% working on both servers and in your localhost.
- Is it illegal? It is not illegal. There are not any restrictions for using this function or code in your site.
- Is there any problem with Adsence eligibility by using this code? No
- I found that this script can't able fetch meta tags from some types of sites, how to solve that? This is because the site denied fetching Meta tags. So we can't able to solve that problem.
Index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fetch meta tags - fluratech</title>
<!-- Designed by Fluratech.com -->
<!-- style-->
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Fetch meta details</h1>
<div class="mainContainer">
<form action="#" method="GET">
<input type="url" name="sitesUrl" required>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_GET["submit"])) {
$domainurl = $_GET["sitesUrl"];
// PREDEFINED FUNCTION
$metatag = get_meta_tags($domainurl);
$description = $metatag['description'];
$viewport = $metatag['viewport'];
$keywords = $metatag['keywords'];
$rating = $metatag['rating'];
$pagename = $metatag['application-name'];
$robots = $metatag['robots'];
$googlesiteverification = $metatag['google-site-verification'];
$tableCon ="<div class='scroller'><table class='helpsection'>
<tr><th>meta tag</th><th>Details</th></tr>
<tr><td>Description</td><td>$description</td></tr>
<tr><td>Viewport</td><td>$viewport</td></tr>
<tr><td>Keyword</td><td>$keywords</td></tr>
<tr><td>Robots</td><td>$robots</td></tr>
<tr><td>Rating</td><td>$rating</td></tr>
<tr><td>Page name</td><td>$pagename</td></tr>
<tr><td>Google site verification</td><td>$googlesiteverification</td></tr>
</table></div>";
}
echo $tableCon;
?>
</div>
</body>
</html>
To display all meta tags
If you want to display all the Meta tags of a website. Paste this code below echo $tableCon;
echo "<pre>"; print_r($metatag); echo "</pre>";
Style.css
Make this layout more awesome using css.
*{
margin: 0%;
padding: 0%;
box-sizing: border-box;
text-align: center;
}
body{
background-image: linear-gradient(to right, rgb(136, 0, 118),rgb(238, 255, 0),rgb(6, 28, 153));
height: 100%;
width: 100%;
}
h1{
color: white;
padding: 10px;
}
.mainContainer{
max-width: 700px;
margin: auto;
background-color: white;
padding: 30px;
}
Conclusion
I hope that this post will help your PHP project. If this post is useful for you, don't forget to share and comment on your valuable opinion.
Post a Comment