Skip to main content

Fetch Meta Data from URL using PHP and Ajax


In this tutorial am going to explain how to get meta data from an URL to display in website. It̢۪s similar to URL data extracting. You can see this example most of social networking sites like Facebook and Google Plus. It also called as Extracting data from URL using Jquery, Ajax and PHP.

I have used get_meta_tags PHP function to get the Meta data from URL. This is a PHP in built function. The output will be returned in array.

    $url=get_meta_tags("http://www.webinfopedia.com/"); 

This will give you an array of all Meta data like Description, Keyword, Author, Copyright, Robot etc… Now you need to display it like below.


    echo ($url["description"]);
    echo substr($url["keywords"], 0, 128); 
    echo ($url["copyright"]);

To get the title tag form URL am using file_get_contents function.


    echo get(file_get_contents('http://www.webinfopedia.com/'), "");
    //get function 
    function get($a,$b,$c)
    {
     // Gets a string between 2 strings
    $y = explode($b,$a);
    $x = explode($c,$y[1]);
    return $x[0];
    }
Using same this method you can also fetch the first image from URL. Check the function below:


    echo get(file_get_contents(''.$_REQUEST['url'].''), "");


<?php
 $str = '<a href="https://www.w3schools.com">Go to w3schools.com</a>';
 echo htmlentities($str);
 ?>
 <!DOCTYPE html>
 <html>
 <body>
 
 <?php
 $str = '<a href="https://www.w3schools.com">Go to w3schools.com</a>';
 echo htmlentities($str);
 ?>
 
 <p>Converting characters into entities are often used to prevent browsers from using it as an HTML element. This can be especially useful to prevent code from running when users have access to display input on your homepage.</p>
 
 </body>
 </html>