I am creating the sitemap in a dynamic way, but when I upload it to Google I receive that error:
https://s7.postimg.org/r3uu7ixy3/Captura_de_pantalla_2017-06-16_a_las_23.43.05.png
How can I fix this?
The code I am using:
<?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:news='http://www.google.com/schemas/sitemap-news/0.9'>";
require 'admin/config.php';
require 'funciones.php';
try {
$conexion = new PDO($bd_config['dbname'], $bd_config['usuario'], $bd_config['password'] );
} catch (PDOException $e) {
header ('Location: error.php');
echo "ERROR: ".$e->getMessage();
die();
}
//CODE
echo "</urlset>\n";
?>
I’m surprised you didn’t get a “headers already sent” error.
Anyway, AFAIK the newline between the opening <?php
and header
will send output headers, presumably the default “text/html”
Try removing that newline.
The space between <?php
and header
you mean?
Yes, try like this
<?php
header("Content-type: text/xml");
.....
You can try changing “text/xml” to “application/xml” but I have a feeling there is something else happening.
If you use your browser to go to the sitemap page and look at the view-source, it looks OK?
Is this for a WordPress site running the W3 Total Cache plugin and the hide sitemap setting is checked?
I was using application/xml
but with the same result. I´m not using WordPress, and the view source looks fine, but I keep with that error on Google Site.
Try this:
<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('html_errors', 'true');
ini_set('display_errors', 'true');
# header("Content-type: text/xml");
$result = xmlHeader();
for( $i2=0; $i2<5; $i2++):
$url = 'http://example-' .$i2 .'.com';
$date = date('Y-m-d'); // '2017-06-17';
$changeFreq = 'daily';
$priority = '0.9';
$result .= insertUrl($url, $date, $changeFreq, $priority);
endfor;
$result .="\n</urlset>\n";
/*
require 'admin/config.php';
require 'funciones.php';
try {
$conexion = new PDO($bd_config['dbname'], $bd_config['usuario'], $bd_config['password'] );
} catch (PDOException $e) {
header ('Location: error.php');
echo "ERROR: ".$e->getMessage();
die();
}
//CODE
*/
$xmlName = 'Sitemap.xml';
$ok = xmlFileSave($result, $xmlName);
$msg = 'Problem in saving file: ';
if($ok):
$msg = 'Just Saved: ' . $xmlName;
fred($result, $msg);
$msg = '';
endif;
echo $msg;
echo '<h1> ========= Finished ========= </h1>';
//=====================================================
// ONLY FUNCTION BELOW
//=====================================================
function xmlHeader()
{
// Google Template:
// https://support.google.com/webmasters/bin/answer.py?hl=en&answer=183668&ctx=cb&src=cb&cbid=dxt8y22yvlcb&cbrank=3
// $lines[] = '< ? xml version="1.0" encoding="UTF-8" ? >';
$result = <<< RESULT
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="https://www.sitemaps.org/schemas/sitemap-image/1.1"
xmlns:video="https://www.sitemaps.org/schemas/sitemap-video/1.1">
RESULT;
return $result;
}
//=====================================================
function insertUrl($url, $date, $changeFreq, $priority)
{
$result = <<< ____RESULT
<url>
<loc> $url </loc>
<lastmod> $date </lastmod>
<changefreq> $changeFreq </changefreq>
<priority> $priority </priority>
</url>
____RESULT;
return $result;
}///
//=====================================================
function xmlFileSave($lines, $FILENAME)
{
$result = false;
$file = fopen( $FILENAME, "w+") or exit("Unable to open file: " . $FILENAME);
$file_size = file_put_contents($FILENAME, $lines);
$result = fclose($file);
return $result;
}///
//=====================================================
function fred($var, $title='')
{
echo '<pre>' .$title .'<br>';
print_r($var);
echo '</pre>';
}///
# NOT REQUIRED ? >
#### Results which validate using three different validators:
```
<?xml version="1.0" encoding="UTF-8" ?>
http://example-0.com
2017-06-17
daily
0.9
http://example-1.com
2017-06-17
daily
0.9
http://example-2.com
2017-06-17
daily
0.9
http://example-3.com
2017-06-17
daily
0.9
http://example-4.com
2017-06-17
daily
0.9
Thanks for your time. I don’t really understand where should I use this code.
Try copying to the same directory where you have your file, call it something similar to test-001.php. Run php the file to produce the XML file and see if it passes the Google validation.
If it validates ok, rem out the for/next loop and in insert your while loop and call
$result .= insertUrl($url, $date, $changeFreq, $priority);
I have tried just copying the code but is not well formed or something similar, and I can’t make it run.
John_Betong:
declare(strict_types=1)
Try remming that declaration and report back with any errors or the reason why the XML file is not being produced.
Edit:
Is there anybody else who could try this script.
It is hard work trying to debug on a tablet
I have run it on PHP 5.6 and get
Warning: Unsupported declare 'strict_types' in ...\johnb.php on line 2
Just Saved: Sitemap.xml
http://example-0.com
2017-06-17
daily
0.9
http://example-1.com
2017-06-17
daily
0.9
http://example-2.com
2017-06-17
daily
0.9
http://example-3.com
2017-06-17
daily
0.9
http://example-4.com
2017-06-17
daily
0.9
========= Finished =========
1 Like
Thank you, looks as though it is working ok even with the php7 declaration warning.
1 Like
I´m sorry, but I don’t know what exactly I have to do.
I thought that only applied to blank lines before the PHP opening line, not those after it? O/T, but I just wondered.
It ran OK for me and passed W3C validation
I just tried and that does seem to be the case, though it throws a “XML declaration allowed only at the start of the document” not an “already sent” with a newline before the <?php
I would have bet that newlines inside <?php
before header() sent output, but either I’ve been wrong all this time or things have changed over the years.
1 Like
I copied your onsite xml file, reduced the number of links and created the following test-file.xml which passes the Google validation tests.
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="https://www.sitemaps.org/schemas/sitemap-image/1.1"
xmlns:video="https://www.sitemaps.org/schemas/sitemap-video/1.1">
<url>
<loc>http://www.laxtore.com/</loc>
<lastmod>2017-06-18</lastmod>
<changefreq>daily</changefreq>
<priority>1</priority>
</url><url>
<loc>http://www.laxtore.com/Juegos/Indice-Precios/</loc>
<lastmod>2017-06-18</lastmod>
<changefreq>daily</changefreq>
<priority>1</priority>
</url><url>
<loc>http://www.laxtore.com/contacto/</loc>
<priority>0.8</priority>
</url><url>
<loc>http://www.laxtore.com/Juegos/Xbox-One/</loc>
<lastmod>2017-06-18</lastmod>
<changefreq>daily</changefreq>
<priority>1</priority>
</url>
</urlset>
Please note the **differences** between this **test-file.xml** and yours :)
Also try saving the test-file.xml to your site and test the Google validation.
1 Like
system
Closed
September 17, 2017, 3:48pm
19
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.