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.
<?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("&", "&", $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("&", "&", $judul);
$judul = str_replace(" ", "", $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(" ", "", $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!';
}
}
<?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("&", "&", $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("&", "&", $judul);
$judul = str_replace(" ", "", $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(" ", "", $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!';
}
}
}
<?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;
}
}
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;
http://localhost/ci4/xml/sitemap
http://localhost/ci4/xml/feed