Source Code Membuat sitemap.xml dan feed.xml dengan Codeigniter 4

Source Code Membuat sitemap.xml dan feed.xml dengan Codeigniter 4

Kali ini Rumah Code akan maembahas bagaiman cara membuat file xml dengan php & Mysql. Kebetulan Rumah Code sedang belajar Framework Codeigniter 4 maka tutorial ini akan di buat dengan menggunakan framework CI4. Sebelum memulai seting lah Codeigniter 4 dengan menggunakan cara yang telah dibahas oleh Rumah Code. Tutorial yang di gunakan bisa menggunakan mode virtual host atau yang biasa

  1. Buatlah Model Xml.php
    <?php 
    namespace App\Controllers;
    use App\Models\M_db;
    use CodeIgniter\Controller;
    helper('filesystem');
    
    class Xml extends Controller
    {		
    protected $helpers = ['url'];
    
    .......
    }​
  2. Buat fungsi sitemap pada model xml controler dengan cara menyisipkan kode berikut dengan mengganti ....... dengan code di bawah ini
    	public function sitemap()
    	{	
    		$std = new M_db();
    		date_default_timezone_set('Asia/Jakarta');
    		$a = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
    		$a .= '<urlset'."\n";
    		$a .= '        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n";
    		$a .= '        xmlns:xhtml="http://www.w3.org/1999/xhtml"'."\n";
    		$a .= '        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"'."\n";
    		$a .= '        xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">	"'."\n";
    		$table ='artikel';
    		$field = '*';
    		$data['artikel']=$std->list_semua($table,$field);
    		$t=time();
    		$skr=date("Y-m-d",$t).'T'.date("h:i:s",$t);
    		$a .= '<url>'."\n";
    		   $a .= '<loc>https://www.rumahcode.org/</loc>'."\n";
    		   $a .= '<lastmod>'.$skr.'+00:00</lastmod>'."\n";
    		   $a .= '<changefreq>daily</changefreq>'."\n";
    		   $a .= '<priority>1.0000</priority>'."\n";
    		$a .= '</url>'."\n";
    		$a .= '<url>'."\n";
    		   $a .= '<loc>https://www.rumahcode.org/blog</loc>'."\n";
    		   $a .= '<lastmod>'.$skr.'+00:00</lastmod>'."\n";
    		   $a .= '<changefreq>daily</changefreq>'."\n";
    		   $a .= '<priority>1.0000</priority>'."\n";
    		$a .= '</url>'."\n";
    		$a .= '<url>'."\n";
    		   $a .= '<loc>https://www.rumahcode.org/tools/myipaddress</loc>'."\n";
    		   $a .= '<lastmod>'.$skr.'+00:00</lastmod>'."\n";
    		   $a .= '<changefreq>daily</changefreq>'."\n";
    		   $a .= '<priority>1.0000</priority>'."\n";
    		$a .= '</url>'."\n";
    		$a .= '<url>'."\n";
    		   $a .= '<loc>https://www.rumahcode.org/tools/nslookup</loc>'."\n";
    		   $a .= '<lastmod>'.$skr.'+00:00</lastmod>'."\n";
    		   $a .= '<changefreq>daily</changefreq>'."\n";
    		   $a .= '<priority>1.0000</priority>'."\n";
    		$a .= '</url>'."\n";
    		foreach ($data['artikel'] as $item):
    		$a .= '<url>'."\n";
    		$judul = str_replace(" ", "-", $item->artikel_judul);
    		$judul = str_replace("<?php", "", $judul);
    		$judul = str_replace("&", "&amp;", $judul);
    		   $a .= '<loc>https://www.rumahcode.org/blog/'.$item->artikel_id.'/'.$judul.'</loc>'."\n";
    		   $a .= '<lastmod>'.$skr.'+00:00</lastmod>'."\n";
    		   $a .= '<changefreq>daily</changefreq>'."\n";
    		   $a .= '<priority>1.0000</priority>'."\n";
    		$a .= '</url>'."\n";
    		endforeach;
    		$a .= '</urlset>';
    				header('Content-Type: application/xml');
    			if ( ! write_file('./sitemap.xml', $a))
    			{
    			        echo 'Unable to write the file';
    			}
    			else
    			{
    			        echo 'File written!';
    			}
    	}
  3. Buatlah fungsi feed di bawah fungsi sitemap untuk membuat feed.xml. Dengan menyisipkan kode berikut
    	public function feed()
    	{	
    		$std = new M_db();
    		date_default_timezone_set('Asia/Jakarta');
    		$mulai = 0;
    		$perpage = 10;
    		$table ='artikel';
    		$field = '*';
    		$data['artikel']=$std->list_tb($table,$field,$mulai,$perpage);
    		$b = '<?xml version="1.0"?>'."\n";
    		$b .= '<rss version="2.0">'."\n";
    		$b .= '    <channel>'."\n";
    		$b .= '        <title>RumahCode.Org</title>'."\n";
    		$b .= '        <link>https://www.rumahcode.org/</link>';
    		$b .= '        <description>Rumah Code ini dibuat sebagai tempat sharing dan media belajar tentang bahasa pemograman web terutama php dan Sistem Opreasi Linux Terutama Centos</description>'."\n";
    		$b .= '        <language>id</language>'."\n";
    		$b .= '        <pubDate>Tue, 3 Dec 2019 12:00:00 GMT</pubDate>'."\n";
    
    		$b .= '        <lastBuildDate>'.date(DATE_RFC822).'</lastBuildDate>'."\n";
    		$b .= '        <docs>http://blogs.law.harvard.edu/tech/rss</docs>'."\n";
    		$b .= '        <generator>Weblog Editor 2.0</generator>'."\n";
    		$b .= '        <managingEditor>black.scorpio.night@gmail.com</managingEditor>'."\n";
    		$b .= '        <webMaster>black.scorpio.night@gmail.com</webMaster>'."\n";
    		foreach ($data['artikel'] as $item):
    		$b .= '        <item>'."\n";
    		$judul = str_replace(" ", "-", $item->artikel_judul);
    		$judul = str_replace("<?php", "", $judul);
    		$judul = str_replace("&", "&amp;", $judul);	
    		$judul = str_replace("&nbsp;", "", $judul);	
    		
    		$b .= '            <title>'.$judul.'| Rumah Code</title>'."\n";
    		$b .= '            <link>https://www.rumahcode.org/blog/'.$item->artikel_id.'/'.$judul.'</link>'."\n";
    		$b .= '            <description>'."\n";
    		$b .=                 str_replace("&nbsp;", "", $item->artikel_deskripsi)."\n";
    		$b .= '            </description>'."\n";
    		$b .= '            <guid></guid>'."\n";
    		$b .= '        </item>'."\n";
    		endforeach;
    		$b .= '        <item>'."\n";	
    		$b .= '            <title>WHAT IS MY IPADDRESS | Rumah Code</title>'."\n";
    		$b .= '            <link>https://www.rumahcode.org/tools/myipaddress</link>'."\n";
    		$b .= '            <description>'."\n";
    		$b .=                 'What Is My Ip Address adalah sebuah Tools untuk mengecek Ip Address Anda'."\n";
    		$b .= '            </description>'."\n";
    		$b .= '            <guid></guid>'."\n";
    		$b .= '        </item>'."\n";
    		$b .= '        <item>'."\n";	
    		$b .= '            <title>NSLOOKUP ONLINE | Rumah Code</title>'."\n";
    		$b .= '            <link>https://www.rumahcode.org/tools/nslookup</link>'."\n";
    		$b .= '            <description>'."\n";
    		$b .=                 'Nslookups Online adalah sebuah Tools untuk ipaddress suatu domain'."\n";
    		$b .= '            </description>'."\n";
    		$b .= '            <guid></guid>'."\n";
    		$b .= '        </item>'."\n";
    		$b .= '    </channel>'."\n";
    		$b .= '</rss>'."\n";
    		header('Content-Type: application/xml');
    							if ( ! write_file('./feed.xml', $b))
    			{
    			        echo 'Unable to write the feed';
    			}
    			else
    			{
    			        echo 'fedd written!';
    			}
    	}
  4. Sehingga model xml.php menjadi sperti di bawah ini.
    <?php 
    namespace App\Controllers;
    use App\Models\M_db;
    use CodeIgniter\Controller;
    helper('filesystem');
    
    
    class Xml extends Controller
    {	
    	
    protected $helpers = ['url'];
    
    	public function sitemap()
    	{	
    		$std = new M_db();
    		date_default_timezone_set('Asia/Jakarta');
    		$a = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
    		$a .= '<urlset'."\n";
    		$a .= '        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n";
    		$a .= '        xmlns:xhtml="http://www.w3.org/1999/xhtml"'."\n";
    		$a .= '        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"'."\n";
    		$a .= '        xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">	"'."\n";
    		$table ='artikel';
    		$field = '*';
    		$data['artikel']=$std->list_semua($table,$field);
    		$t=time();
    		$skr=date("Y-m-d",$t).'T'.date("h:i:s",$t);
    		$a .= '<url>'."\n";
    		   $a .= '<loc>https://www.rumahcode.org/</loc>'."\n";
    		   $a .= '<lastmod>'.$skr.'+00:00</lastmod>'."\n";
    		   $a .= '<changefreq>daily</changefreq>'."\n";
    		   $a .= '<priority>1.0000</priority>'."\n";
    		$a .= '</url>'."\n";
    		$a .= '<url>'."\n";
    		   $a .= '<loc>https://www.rumahcode.org/blog</loc>'."\n";
    		   $a .= '<lastmod>'.$skr.'+00:00</lastmod>'."\n";
    		   $a .= '<changefreq>daily</changefreq>'."\n";
    		   $a .= '<priority>1.0000</priority>'."\n";
    		$a .= '</url>'."\n";
    		$a .= '<url>'."\n";
    		   $a .= '<loc>https://www.rumahcode.org/tools/myipaddress</loc>'."\n";
    		   $a .= '<lastmod>'.$skr.'+00:00</lastmod>'."\n";
    		   $a .= '<changefreq>daily</changefreq>'."\n";
    		   $a .= '<priority>1.0000</priority>'."\n";
    		$a .= '</url>'."\n";
    		$a .= '<url>'."\n";
    		   $a .= '<loc>https://www.rumahcode.org/tools/nslookup</loc>'."\n";
    		   $a .= '<lastmod>'.$skr.'+00:00</lastmod>'."\n";
    		   $a .= '<changefreq>daily</changefreq>'."\n";
    		   $a .= '<priority>1.0000</priority>'."\n";
    		$a .= '</url>'."\n";
    		foreach ($data['artikel'] as $item):
    		$a .= '<url>'."\n";
    		$judul = str_replace(" ", "-", $item->artikel_judul);
    		$judul = str_replace("<?php", "", $judul);
    		$judul = str_replace("&", "&amp;", $judul);
    		   $a .= '<loc>https://www.rumahcode.org/blog/'.$item->artikel_id.'/'.$judul.'</loc>'."\n";
    		   $a .= '<lastmod>'.$skr.'+00:00</lastmod>'."\n";
    		   $a .= '<changefreq>daily</changefreq>'."\n";
    		   $a .= '<priority>1.0000</priority>'."\n";
    		$a .= '</url>'."\n";
    		endforeach;
    		$a .= '</urlset>';
    				header('Content-Type: application/xml');
    			if ( ! write_file('./sitemap.xml', $a))
    			{
    			        echo 'Unable to write the file';
    			}
    			else
    			{
    			        echo 'File written!';
    			}
    	}
    	public function feed()
    	{	
    		$std = new M_db();
    		date_default_timezone_set('Asia/Jakarta');
    		$mulai = 0;
    		$perpage = 10;
    		$table ='artikel';
    		$field = '*';
    		$data['artikel']=$std->list_tb($table,$field,$mulai,$perpage);
    		$b = '<?xml version="1.0"?>'."\n";
    		$b .= '<rss version="2.0">'."\n";
    		$b .= '    <channel>'."\n";
    		$b .= '        <title>RumahCode.Org</title>'."\n";
    		$b .= '        <link>https://www.rumahcode.org/</link>';
    		$b .= '        <description>Rumah Code ini dibuat sebagai tempat sharing dan media belajar tentang bahasa pemograman web terutama php dan Sistem Opreasi Linux Terutama Centos</description>'."\n";
    		$b .= '        <language>id</language>'."\n";
    		$b .= '        <pubDate>Tue, 3 Dec 2019 12:00:00 GMT</pubDate>'."\n";
    
    		$b .= '        <lastBuildDate>'.date(DATE_RFC822).'</lastBuildDate>'."\n";
    		$b .= '        <docs>http://blogs.law.harvard.edu/tech/rss</docs>'."\n";
    		$b .= '        <generator>Weblog Editor 2.0</generator>'."\n";
    		$b .= '        <managingEditor>black.scorpio.night@gmail.com</managingEditor>'."\n";
    		$b .= '        <webMaster>black.scorpio.night@gmail.com</webMaster>'."\n";
    		foreach ($data['artikel'] as $item):
    		$b .= '        <item>'."\n";
    		$judul = str_replace(" ", "-", $item->artikel_judul);
    		$judul = str_replace("<?php", "", $judul);
    		$judul = str_replace("&", "&amp;", $judul);	
    		$judul = str_replace("&nbsp;", "", $judul);	
    		
    		$b .= '            <title>'.$judul.'| Rumah Code</title>'."\n";
    		$b .= '            <link>https://www.rumahcode.org/blog/'.$item->artikel_id.'/'.$judul.'</link>'."\n";
    		$b .= '            <description>'."\n";
    		$b .=                 str_replace("&nbsp;", "", $item->artikel_deskripsi)."\n";
    		$b .= '            </description>'."\n";
    		$b .= '            <guid></guid>'."\n";
    		$b .= '        </item>'."\n";
    		endforeach;
    		$b .= '        <item>'."\n";	
    		$b .= '            <title>WHAT IS MY IPADDRESS | Rumah Code</title>'."\n";
    		$b .= '            <link>https://www.rumahcode.org/tools/myipaddress</link>'."\n";
    		$b .= '            <description>'."\n";
    		$b .=                 'What Is My Ip Address adalah sebuah Tools untuk mengecek Ip Address Anda'."\n";
    		$b .= '            </description>'."\n";
    		$b .= '            <guid></guid>'."\n";
    		$b .= '        </item>'."\n";
    		$b .= '        <item>'."\n";	
    		$b .= '            <title>NSLOOKUP ONLINE | Rumah Code</title>'."\n";
    		$b .= '            <link>https://www.rumahcode.org/tools/nslookup</link>'."\n";
    		$b .= '            <description>'."\n";
    		$b .=                 'Nslookups Online adalah sebuah Tools untuk ipaddress suatu domain'."\n";
    		$b .= '            </description>'."\n";
    		$b .= '            <guid></guid>'."\n";
    		$b .= '        </item>'."\n";
    		$b .= '    </channel>'."\n";
    		$b .= '</rss>'."\n";
    		header('Content-Type: application/xml');
    							if ( ! write_file('./feed.xml', $b))
    			{
    			        echo 'Unable to write the feed';
    			}
    			else
    			{
    			        echo 'fedd written!';
    			}
    	}	
    }
  5. Karena kita mengambil data dari mysql dan menggunakan model M_db maka kita harus membuat model M_db. Dan isi model tersebut dengan kode di bawah ini
    <?php namespace App\Models;
    
    use CodeIgniter\Model;
    
    class M_db extends Model
    {
        function list_semua($table,$field)
        {   $db = \Config\Database::connect();
            $sql = 'SELECT '.$field.' FROM '.$table.' where artikel_pub = 1';
            $query = $db->query($sql);
            $results = $query->getResult();
            return $results;
        } 
        function list_tb($table,$field,$mulai,$perpage)
        {	$db = \Config\Database::connect();
        	$sql = 'SELECT '.$field.' FROM '.$table.' where artikel_pub = 1  ORDER BY artikel_id DESC limit '.$mulai.','.$perpage;
            
        	$query = $db->query($sql);
        	$results = $query->getResult();
    
        	return $results;
        }
    }​
  6. Contoh ini mengambil data dari tabel artikel dengan struktur seperti ini
    CREATE TABLE `artikel` (
      `artikel_id` bigint(20) NOT NULL,
      `artikel_judul` varchar(100) NOT NULL,
      `artikel_deskripsi` text NOT NULL,
      `artikel_tgl` date NOT NULL,
      `artikel_pub` tinyint(1) DEFAULT 1
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;​
  7. Untuk menjalankan dan mengetes source code di atas kamu dapat mengakses alamat:
    1. Untuk membuat sitemap.xml di akses melalui http://localhost/ci4/xml/sitemap
    2. Untuk membuat feed.xml dapat di akses melalui http://localhost/ci4/xml/feed
Artikel Menarik Lainnya
Codeigniter 4, Codeigniter, PHP, MySql

loading...