Wednesday, October 11, 2017

Anil kr Prajapati

How to Get Title, Description, and Keywords of a Website in PHP ?

I found most of the people asking for a way to get meta data of a website, so I thought to share the logic with a very easy PHP script that will retrieve a website page title, keywords and description. You can also get the sitemap of a website url. Here I am describing it with proper UI and with output. So follow these steps.

Step #1 : First design a page where you can input website URL.

Copy index.php Code :-

<form action="show.php" method="POST">
    <table cellspacing="0" height="38%" width="57%" style="border:1px solid;">
            <tr><td width="27%">Enter URL:</td><td width="50%"><input class="input" type="text" id="url" name="url"></td></tr>                     
                        <tr> <td></td><td><input type="submit" name="submit" value="submit" /></td></tr>

Step #2 : Now add below code in new page show.php.

 //$weburl variable to store url
    $html = file_get_contents_curl("$weburl");
    //parsing begins here:
    $doc = new DOMDocument();
   $nodes = $doc->getElementsByTagName('title');
   //get and display data:
   $title = $nodes->item(0)->nodeValue;
   $metas = $doc->getElementsByTagName('meta');
   for ($i = 0; $i < $metas->length; $i++)
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
    if($meta->getAttribute('name') == 'keywords')
        $keywords = $meta->getAttribute('content');
     echo "Title: $title". '<br/><br/>';
     echo "Description: $description". '<br/><br/>';
     echo "Keywords: $keywords".'<br/><br/>';
       foreach($links as $link){
         echo "Internal and External Links".": ". $link."<br>";
    echo '<br/>';
    echo "ip address of your webstie:",gethostbyname($weburl). '<br/><br/>'; 
    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $finish = $time;
    $total_time = round(($finish - $start), 4);
   echo'Load time:Page generated in ' . $total_time . ' seconds.';
   echo '<br/><br/>';
   $ch = curl_init($weburl);
   curl_setopt($ch, CURLOPT_HEADER, true);    // we want headers
   curl_setopt($ch, CURLOPT_NOBODY, true);    // we don't need body
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
   curl_setopt($ch, CURLOPT_TIMEOUT,10);
   $output = curl_exec($ch);
   $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

  echo 'HTTP code: ' . $httpcode;

 Step #3 : If submit URL the output will be as below.

So now you can see we have the all meta data of a URL that submit by us. 

You can also do this with helping of video as below..

About Author -

Hi, I am Anil.

Welcome to my eponymous blog! I am passionate about PHP programming. Here you will find a hug of information on web development, web design, PHP and Latest technology.

Subscribe to this Blog via Email :