PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: sitemap
sitemap - phponwebsites.com
Showing posts with label sitemap. Show all posts

25 Aug 2014

Parse/Read data from XML Sitemap using PHP

                                You can Create XML Sitemap dynamically using PHP and MySQL. Then you've have a doubt. How can retrieve data from XML Sitemap using PHP? In this post, You are going to know get data from XML Sitemap using PHP.

                                 You can get value from XML Sitemap using PHP in following two ways.

1. Get data from XML Sitemap using simple_load_file() in PHP
2. Get data from XML Sitemap using file_get_contents() and SimpleXMLElement() in PHP

Lets see below examples.

Get data from XML Sitemap using simple_load_file() in PHP


                                   Simple_load_file() in PHP is used to convert XML document into an object. The below example explain retrieve data from XML Sitempa.


<?php
$xml=simplexml_load_file('http://www.phponwebsites.com/sitemap.xml');
//print_R($xml);
foreach($xml->url as $val)
 {
  echo $val->loc.' '. $val->lastmod.' '. $val->changefreq.' '. $val->priority.'<br>';
 }
?>


Get data from XML Sitemap using file_get_contents and SimpleXMLElement in PHP


                                  File_get_contents() is used to read the contents of a file into string. SimpleXMLElement is used to represents elements of XML document. The below example explain retrieve data from XML Sitemap.

<?php
$url=file_get_contents('http://www.phponwebsites.com/sitemap.xml');
$xml=new SimpleXMLElement($url);
//print_R($xml);
foreach($xml->url as $val)
 {
  echo $val->loc.' '. $val->lastmod.' '. $val->changefreq.' '. $val->priority.'<br>';
 }
?>


 When you give print_R() in above PHP file, you can see the output on screen like below.


 SimpleXMLElement Object (
   [url] => Array (
         [0] => SimpleXMLElement Object (
       [loc] => http://www.phponwebsites.com/p/php.html
           [lastmod] => 2014-07-26
       [changefreq] => daily
       [priority] => 1 )
 [1] => SimpleXMLElement Object (
       [loc] => http://www.phponwebsites.com/p/mysql.html
       [lastmod] => 2014-07-26
       [changefreq] => daily
       [priority] => 1 )
 [2] => SimpleXMLElement Object (
       [loc] => http://www.phponwebsites.com/p/htaccess.html
       [lastmod] => 2014-07-26
       [changefreq] => daily
       [priority] => 1 )
  )
)

Now you can retrieve data from XML Sitemap.

Related Post:
Create Excel file using PHP and MySQL
Create XML Sitemap dynamically using PHP and MySQL
Create RSS Feed Dynamically using PHP and MySQL
PHP : Parse data from RSS Feed using SimpleXML
Read data from JSON file using PHP
Parse data from XML file using PHP

2 Jul 2014

Create sitemap dynamically using PHP and MySQL

                         Do you know why you need to add sitemap into you websites? what is the use of sitemap? Let see here to solve all of your doubts about sitemap.

Sitemap:


                         Sitemap is very important for all websites. All of you know, the search engines like google, bing are crawling pages on your websites to know about websites. Using this sitemap, webmasters to inform search engines about pages for crawling. Sitemap is a XML file which contains all url of your websites upto to date. If you add new page to your site, then you'll add url of new page to sitemap for crawling.

                          Sitemap is also provide url structure of websites. Suppose a new user comes to your websites. The sitemap leads new user to all pages on your site.

Structure of sitemap: 


                    The structure of sitemap is:

<?xml version="1.0" encoding="UTF-8">
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
  <url>
    <loc>http://www.phponwebsites.com</loc>
<lastmod>today date<lastmod>
<changefreq>daily</changefreq>
<priority>1</priority>
  </url>
  <url>
    <loc>http://www.phponwebsites.com/p/htaccess.html</loc>
<lastmod>today date<lastmod>
<changefreq>daily</changefreq>
<priority>1</priority>
  </url>
 </urlset>

  where,
      urlset - encapsulates the file ie, start sitemap
      url - parent tag for each entry ie, each page
      loc - location of url
      lastmod - date for last modification of file
      changefreq -  how frequently the pag is changed. It should be always, hourly, daily, monthly, weekly, yearly, never
      priority - priority of url. It should be between 0.0 to 1.0

Create sitemap dynamically using PHP and MySQL:


                          First you need to add content type for xml to generate sitemap in PHP. ie,

 header ("content-type: text/xml");


                        Then add contents to XML file. Then save this file as sitemap.php .


<?php
 mysql_connect('localhost','root','') or die(mysql_error());
 mysql_select_db('new') or die(mysql_error());
 $dat=date('Y-m-d');
 header ("content-type: text/xml");
 echo '<?xml version="1.0" encoding="UTF-8"?>
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
 $urls=mysql_query("select * from url")or die(mysql_error());
 while($row_Tags=mysql_fetch_array($urls))
 {
   echo "<url><loc>".$row_Tags['url']."</loc><lastmod>".$dat."</lastmod><changefreq>daily</changefreq><priority>1</priority></url>";
 }
  echo "</urlset>";
 ?>


.htaccess:

               
               In your .htaccess, you add following codes.

RewriteRule ^sitemap.xml sitemap.php [L]

                   When you run sitemap.xml on your website each time,  it display upto date contents.

Related Post: