create dynamic xml sitemapl with php - php

I have error:
This page contains the following errors:
error on line 4 at column 6: XML declaration allowed only at the start of
the document
Below is a rendering of the page up to the first error.
<?php include 'config.php';
header("Content-Type: text/xml");
echo '<?xml version="1.0" encoding= "utf-8"?>';
echo '<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">';
echo '<url>
<loc>http://cebhe.info/</loc>
<changefreq>always</changefreq>
</url>';
$get_menus_sql = $db->prepare("SELECT id,ad FROM kateqoriyalar");
$get_menus->execute();
foreach($get_menus_sql as $get_menus){
$menu_name = $get_menus['id'];
$menu_slug = $get_menus['ad'];
if($pagename == $menu_slug)
{$menuselected = 'select';}else{$menuselected = '';}
echo'
<url>
<loc>http://cebhe.info/kat.php?'.$menu_name.'</loc>
<changefreq>always</changefreq>
</url>'; }
$sitemap=$db->prepare("SELECT * FROM yazilar ORDER BY id DESC LIMIT 10");
$sitemap->execute();
foreach($sitemap as $sm){
echo'
<url>
<loc>http://cebhe.info/'.seolink(strip_tags($sm['basliq'])).'-'.$sm['id'].'</loc>
<changefreq>always</changefreq>
</url>
';}
echo '</urlset>';?>

Related

xmlParseEntityRef: no name' warnings while loading xml

Hello stackoverflow community,
this code display the url for my sitemap,
the connection work correctly and display the url list (not very well)
the error code i can see in the header
This page contains the following errors:
error on line 2 at column 18210: xmlParseEntityRef: no name
Below is a rendering of the page up to the first error.
<?php
header('Content-type: application/xml; charset=utf-8') ?>
<?php
echo '<?xml version="1.0" encoding="UTF-8"?>' ?>
<?php
define('DBHOST','localhost');
define('DBUSER','user');
define('DBNAME','database');
define('DBPWD','password');
$connect = new MySQLi(DBHOST,DBUSER,DBPWD,DBNAME)or die(mysqli_error());
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
echo '<url>';
$query = $connect->query("select * from rss");
while($row=$query->fetch_array(MYSQL_ASSOC))
{
echo '<loc>'.$row['link'].'</loc>';
}
echo '</url>';
echo '</urlset>';
?>
the code above output this (does this look correct ?)
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>http://www.exemple.com/d/brown</loc>
<loc>http://www.exemple.com/d/blue</loc>
<loc>http://www.exemple.com/d/red/</loc>
<loc>http://www.exemple.com/d/yellow</loc>
//more lines
</url></urlset>
i would like to know what i did wrong on this code
thanks you very much
(French Pierre)
i fixed the problem ,the error came from one field in the rss table contain a bad character ..., but i have added missing xml parameters
lastmod, changefreq, priority
<?php
define('DBHOST','localhost');
define('DBUSER','user');
define('DBNAME','rss');
define('DBPWD','password');
$connect = new MySQLi(DBHOST,DBUSER,DBPWD,DBNAME)or die(mysqli_error());
header("Content-Type: text/xml;charset=iso-8859-1");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
{
$query = $connect->query("select * from rss");
while($row=$query->fetch_array(MYSQL_ASSOC)) {
$url = $row["link"];
$time = $row["when"];
$lastmod = $time;
$datetime = new DateTime($lastmod);
$timeresult = $datetime->format('Y-m-d\TH:i:sP');
echo "<url>
<loc>$url</loc>
<lastmod>$timeresult</lastmod>
<changefreq>hourly</changefreq>
<priority>0.8</priority>
</url>
";}}
?>
</urlset>
like this the output does not give error anymore
it does pass google validation
thanks you

PHP Sitemap - error on line 31 at column 2: Extra content at the end of the document

so I am wondering why this error is showing up. Ive see this work before but all I did was modify the query a bit. For some reason when I get rid of the php loop it works only with the url and loc tags with php inside them. Some help would be great! There error is on line 31 which is right before the php query and loop start.
<?php
header('Content-type: text/xml');
include 'core/init.php';?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/1.0">
<url>
<loc><?php echo $settings['url']; ?></loc>
</url>
<url>
<loc><?php echo $settings['url'] . 'login'; ?></loc>
</url>
<url>
<loc><?php echo $settings['url']. 'register'; ?></loc>
</url>
<url>
<loc><?php echo $settings['url'] . 'sponsored'; ?></loc>
</url>
<url>
<loc><?php echo $settings['url']. 'premium'; ?></loc>
</url>
<url>
<loc><?php echo $settings['url']. 'contact'; ?></loc>
</url>
<url>
<loc><?php echo $settings['url']. 'about'; ?></loc>
</url>
<?php
$result = $database->query("SELECT DISTINCT `slug` FROM `categories`");
while($row = $result->fetch_object()){
?>
<url>
<loc><?php echo $settings['url'] . 'category/' . $row['slug'] . '/'; ?></loc>
</url>
<?php } ?>
</urlset>
Thanks!
I figured it out. I had some errors in my echo statement. I had to use " instead of ' where it says category/ and just /

XML Parsing Error: XML or text declaration not at start of entity

I have this error in my rss.php
XML Parsing Error: XML or text declaration not at start of entity
Location: http://blah.com/blah/blah/site/rss.php
Line Number 1, Column 3:
and this is shown underneath the error
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><item><title>xzfbcbcxv 123</title><description><p>Description for the rss feed</p></description></item></channel></rss>
The
----------------^
is show between the left side of the page and the ?xml line.
In my rss.php I have
<?php header("Content-type: text/xml"); ?>
<?php include("includes/database.php");?>
<?php global $_TWITTER_TABLE, $_HTTP_ADDRESS, $_NEWS_TABLE; ?>
<?php $str = '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<?php $str .= '<rss version="2.0">';?>
<?php
$str.='<channel>';
$sql = "SELECT * FROM $_NEWS_TABLE LIMIT 5";
$result = mysql_query($sql) or die ($sql."".mysql_error());
while($row = mysql_fetch_object($result)){
$str.= '<item>';
$str.= '<title>'.$row->title. '</title>';
$str.= '<description>'.$row->content. '</description>';
$str.= '</item>';
}
$str.= '</channel>';
?>
<?php $str .= '</rss>';
echo $str;
?>
You have a lot of white space before the declaration. You need to remove it. This can be caused by having closing PHP tags followed by spaces or tabs or new line at the end of included files. You can prevent this by just not closing the PHP tags.
Second look, remove all the closing tags at the top of your document:
<?php // make sure that this is the first character in the file, no spaces before it
header("Content-type: text/xml");
include("includes/database.php");
global $_TWITTER_TABLE, $_HTTP_ADDRESS, $_NEWS_TABLE;
$str = '<?xml version="1.0" encoding="UTF-8"?>';
$str .= '<rss version="2.0">';
$str.='<channel>';
$sql = "SELECT * FROM $_NEWS_TABLE LIMIT 5";
$result = mysql_query($sql) or die ($sql."".mysql_error());
while($row = mysql_fetch_object($result)){
$str.= '<item>';
$str.= '<title>'.$row->title. '</title>';
$str.= '<description>'.$row->content. '</description>';
$str.= '</item>';
}
echo $str;
Also, just a note, the mysql_* functions are deprecated due to security problems. You may want to look at mysqli and PDO
I just remove the space from the top of wp-config file and its works for me...
This could be a problem with the XML parsing White-Space.
I'm also faced this problem, remove all unwanted line spaces in your code,i mean remove all unwanted line spaces before and after the php tags.
There can be more reasons but 95% this is the reason of blank space after or before php tags. <?php ?> .

creating custom namespaces in rss

I wish to create some custom namespaces in my rss feed.
The problem is that I have the rss feed working, but the custom namespace data does not show up. I have got it to this far but if anyone could assist in showing how to get this to work properly ( if at all) it would be greatly appreciated.
below is the code i am using to achieve this to this point.
the reason for custom naming is that I am unable to attach the data to anything else that has been already approved.
<?
//Configure the Basic information
$chan_title = "sponsor";
$chan_desc = "some sponsor data.";
$chan_link = "http://pandafc.elementfx.com/panda_app_page.php";
//$image_url = "logo.gif";
//connect to the Database
include 'connect.php';
$query = "SELECT sp_ID, sp_name, sp_about, sp_website FROM sponsors ORDER BY sp_ID DESC LIMIT 25";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) < 1) {
die("No records");
}
//Build the XML data
$items = '';
while ($row = mysql_fetch_assoc($result)) {
$item_id = $row["sp_ID"];
$item_title = $row["sp_name"];
$item_link = $row["sp_about"];
$item_desc = $row["sp_website"];
$items .= <<<ITEM
<item>
<title>$item_id</title>
<sponsor:sponsor>$item_title</sponsor:sponsor>
<sponsor:about>$item_link</sponsor:about>
<sponsor:Website>$item_desc</sponsor:Website>
</item>
ITEM;
}
$content = <<<CONTENT
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:sponsor="http://pandafc.elementfx.com/nssponsor.xhtml" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>$chan_title</title>
<link>$chan_link</link>
<description>$chan_desc</description>
$items
<atom:link href="http://pandafc.elementfx.com/rssfeed1.php" />
</channel>
</rss>
CONTENT;
//print results on screen
header("Content-type: application/rss+xml");
print $content;
?>
hopefully this can be achieved and others may also find this useful

An invalid character was found in text content. Error processing resource

I got the following error,
An invalid character was found in text content. Error processing resource
when I dynamically created xml file using php.The encoding I used was utf-8.I changed it to ISO-8859-1.The error resolved.But the issue is I am having tamil,hindi content So it is displayed as
à®à®à¯à®°à®¾-à®à¯à®à¯à®à¯-
header('Content-Type: application/xml'); echo '<?xml version="1.0" encoding="UTF-8"?>'."\n"; ?> <urlset xmlns="http://www.google.com/schemas/sitemap/0.84"> <?php $sql = "SELECT * FROM table "; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { ?> <url> <loc>http://example.com/<?php echo $row[2]; ?></loc> <lastmod><?php echo str_replace(' ', 'T', $row['dat']).substr(date("O"), 0, -2).':00'; ?></lastmod> </url> <?php } ?></urlset>
How to solve this?
Regards
Rekha
http://hiox.org
adding code from comment below:
<?
header('Content-Type: application/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<?php
$sql = "SELECT * FROM table ";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
?>
<url>
<loc>http://example.com/<?php echo $row[2]; ?></loc>
<lastmod><?php echo str_replace(' ', 'T', $row['dat']).substr(date("O"), 0, -2).':00'; ?></lastmod>
</url>
<?php } ?>
</urlset>
ISO-8859-1 does not have the coverage over hindi and tamil alphabets

Categories