Codeigniter - sitemap error - php

I am trying to create sitemap in my codeigniter app following this answer
Here is my controller method:
public function siteMap() {
$this->load->helper('url');
$urls = array("test");
$data['urls'] = $urls;
$data['frontend'] = $this->getFronendItems();
$this->load->template('front/site_map.php', $data);
}
And my view:
<?php header('Content-type: text/xml'); ?>
<?= '<?xml version="1.0" encoding="UTF-8" ?>' ?>
<url>
<loc><?= base_url() ?></loc>
<priority>1.0</priority>
</url>
<?php foreach($urls as $url) { ?>
<url>
<loc><?= base_url() . $url ?></loc>
<priority>0.5</priority>
</url>
<?php } ?>
This raises the following error:
This page contains the following errors:
error on line 41 at column 8: Opening and ending tag mismatch: link line 0 and head
Tried to remove the header and the url is just echoing as a string on the screen. What am I doing wrong ?

You should always place a header above any view files,
usually I place mine at the top of a controller method.
public function siteMap() {
header("Content-Type: text/xml;charset=iso-8859-1");
$this->load->helper('url');
$urls = array("test");
$data['urls'] = $urls;
$data['frontend'] = $this->getFronendItems();
$this->load->template('front/site_map.php', $data);
}
in the view file, add
<?php print '<?xml version="1.0" encoding="utf-8"?>';?>
<?php print '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';?>
<url>
<loc><?=base_url()?></loc>
<priority>1.0</priority>
</url>
<?php foreach($urls as $url) {
?>
<url>
<loc><?=base_url().$url?></loc>
<priority>0.5</priority>
</url>
<?php
}
?>
<?php print '</urlset>';?>
This is exactly how I do my sitemap.

Related

We create a dynamic sitemap in CodeIgniter but in view it will treat my XML file as HTML

How do I set the route for our sitemap? Currently we set header in our controller, but in the view, it will show as plain text.
controller code
public function sitemap(){
$data = [];
$model = new BlogModel();
$data[‘blogs’] = $model->where(‘STATUS’, ‘1’)->orderBy(‘ID’, ‘DESC’)->findAll();
return view(‘sitemap’, $data);
}
sitemap code
<?php echo ‘<?xml version=”1.0" encoding=”UTF-8"?>’; ?>
<urlset
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
xmlns:image=”http://www.google.com/schemas/sitemap-image/1.1"
xsi:schemaLocation=”http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9">
<! — created with Free Online Sitemap Generator www.xml-sitemaps.com →
<url>
<loc><?= base_url();?></loc>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<?php foreach($blogs as $blog){
$cat_id = $blog[‘PAGE_ID’];
$db = \Config\Database::connect();
$query = $db->query(“SELECT * from mcms_links WHERE ID = ‘$cat_id’”);
$result = $query->getRowArray();
?>
<url>
<loc><?=base_url();?>/<?=$result[‘VALID_NAME’];?>/<?=$blog[‘VALID_NAME’];?></loc>
<changefreq>daily</changefreq>
<priority>1.00</priority>
</url>
<?php }
?>
</urlset>
routes
$routes->get(‘sitemap\.xml’, ‘Sitemap::sitemap’);

Add string to sitemap.xml file inside <urlset> tag in PHP

I tried to add a string into the sitemap.xml file inside <urlset> tag, but it stores differently.
<?php
$date_mod = date('Y-m-d');
$sitemap = "<url>
<loc>http://www.website.com/article.php?page=3</loc>
<lastmod>$date_mod</lastmod>
<priority>0</priority>
</url>";
$xml = simplexml_load_file("sitemap.xml");
$xml->addChild($sitemap);
file_put_contents("sitemap.xml", $xml->asXML());
?>
The output is like:
<?xml version="1.0"?>
<urlset>
<url>
<loc>http://www.website.com/article.php?page=3</loc>
<lastmod>2018-01-12</lastmod>
<priority>0</priority>
</url>
<//www.website.com/article.php?page=3</loc>
<lastmod>2018-01-12</lastmod>
<priority>0</priority>
</url>/></urlset>
Please help me.
If the raw xml is like this:
<?xml version="1.0"?>
<urlset>
</urlset>
And you updated xml is like this:
<?xml version="1.0"?>
<urlset>
<url>
<loc>http://www.website.com/article.php?page=3</loc>
<lastmod>2018-01-12</lastmod>
<priority>0</priority>
</url>
</urlset>
Then you could refer to the following code:
<?php
$date_mod = date('Y-m-d');
$sitemap = "<url>
<loc>http://www.website.com/article.php?page=3</loc>
<lastmod>$date_mod</lastmod>
<priority>0</priority>
</url>";
$sitemap_node =simplexml_load_string($sitemap);
$xml = simplexml_load_file("sitemap.xml");
sxml_append($xml,$sitemap_node);
$xml->asXML('sitemap.xml');
function sxml_append(SimpleXMLElement $to, SimpleXMLElement $from) {
$toDom = dom_import_simplexml($to);
$fromDom = dom_import_simplexml($from);
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}
?>
Your previous code failed to do that is because addChild method can only deal with text (and stil has some drawbacks), not another xml object.

How to print or show sitemap.php data on sitemap.xml?

This is my code on file sitemap.php for my dynamic sitemap but I want to print or show this whole page data on "sitemap.xml" because of Google always consider sitemap.xml for sitemap.This page is working fine but I want all data on sitemap.xml.
How to do this?
<?php
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
include 'includes/connectdb.php';
?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
<url>
<loc>http://www.mobilesapp.com/</loc>
<priority>1.00</priority>
</url>
<?php
$stmt = $con->prepare("SELECT url,modified FROM localnews");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row){
?>
<url>
<loc>http://www.mobilesapp.com/<?= $row['url'] ;?></loc>
<lastmod><?= $row['modified']; ?></lastmod>
<changefreq>always</changefreq>
<priority>1</priority>
</url>
<?php } ?>
<?php
$stmt = $con->prepare("SELECT url,modified FROM socialnews");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row){
?>
<url>
<loc>http://www.mobilesapp.com/<?= $row['url'] ;?></loc>
<lastmod><?= $row['modified']; ?></lastmod>
<changefreq>always</changefreq>
<priority>1</priority>
</url>
<?php } ?>
</urlset>
<?php
$xml = '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">';
// mysql select
foreach ($rows as $row) {
// add next part to xml
$xml .= '
<url>
<loc>http://www.mobilesapp.com/'.$row['url'].'</loc>
<lastmod>'.$row['modified'].'</lastmod>
<changefreq>always</changefreq>
<priority>1</priority>
</url>
';
}
$xml .= '</urlset>';
// show
echo $xml;
// and save to file
file_put_contents('sitemap.xml', $xml);
?>

How to create a sitemap in CodeIgniter

I am trying to create a sitemap in codeigniter. But I am facing some errors please help to fix this.
Here is the sitemap.php code
<?php 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">
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->
<url>
<loc>http://example.com/</loc>
<priority>1.0</priority>
</url>
<url>
<loc>http://example.com/contact</loc>
<priority>1.0</priority>
</url>
</urlset>
Controller
public function sitemap(){
$data = "";//select urls from DB to Array
header("Content-Type: text/xml;charset=iso-8859-1");
$this->load->view("sitemap",$data);
}
ERROR
XML Parsing Error: XML or text declaration not at start of entity
Location: http://localhost/hotel/index.php/sitemap.xml
Line Number 2, Column 1:<?xml version="1.0" encoding="UTF-8"?>
^

How can I add a Child in my xml file with fwrite

I have a problem when I try to insert a new node to my xml file. I need to add:
<ul><loc> mylink</ul></loc>
before the last node in my xml file.
I tried the code below, but it doesn't work and it breaks the last node before I insert the new one:
$userfile = "sitemap.xml";
$fh = fopen($userfile, 'r+');
$addUser = "<url><loc>https://www.example.com/Privacy-Policy</loc></url></urlset> ";
fseek($fh, -10, SEEK_END);
fwrite($fh, $addUser);
fclose($fh);
This is part of my xml file:
<urlset >
<url>
<loc>example.com</loc>
</url>
<url>
<loc>example.com</loc>
</url>
</urlset>
my output is:
<urlset >
<url>
<loc>example1.com</loc>
</url>
<url>
<loc>example2.com</loc>
</ur<url> //<-- See here my xml file broke
<loc>example2.com</loc>
</url>
</urlset>
This should work for you:
(Here I just load the xml file with simplexml_load_file(), to create a SimpleXMLElement(). After this you can simply add a child to the root node)
<?php
$xml = simplexml_load_file("file.xml");
$xml = new SimpleXMLElement($xml->asXML());
$urlChild = $xml->addChild("url", "");
$urlChild->addChild("loc", "example2.com");
$xml->asXML("file.xml");
?>
input file:
<urlset >
<url>
<loc>example.com</loc>
</url>
<url>
<loc>example.com</loc>
</url>
</urlset>
output file:
<?xml version="1.0"?>
<urlset>
<url>
<loc>example.com</loc>
</url>
<url>
<loc>example.com</loc>
</url>
<url>
<loc>example2.com</loc>
</url>
</urlset>

Categories