I need a Dynamic Sitemap for my Dynamic PHP SQL Website, than I searched and followed a tutorial.
But when I submit it, there is an error:
"error on line 1 at column 6: XML declaration allowed only at the start of the document"
I search about ii and I realized that this is because of the short tags of PHP
But I can´t take the Short Tags Function off, because I use that in my whole website.
Then I need another solution;
That´s my code:
<?php
header('Content-type: application/xml; charset=UTF-8');
error_reporting(0);
include "connection.php";
$hoje = date('Y-m-d');
$output = '<?xml version="1.0" encoding="UTF-8"?>';
echo $output
?>
<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">
// Here goes my Normal Sitemap //
// Here goes my Dynamic Sitemap //
<?php
$sql_tabela = mysql_query("SELECT * FROM imovel WHERE codigoImovel != '1' ORDER BY idImovel DESC");
$sql_tabela1 = mysql_query("SELECT * FROM prontomorar WHERE idPronto != '1' ORDER BY idPronto DESC");
$sql_tabela2 = mysql_query("SELECT * FROM construcao WHERE idConstrucao != '1' ORDER BY idConstrucao DESC");
?>
</urlset>
I tried to use that:
echo file_get_contents( "data.txt" );
with this content in "data.txt":
<?xml version="1.0" encoding="UTF-8"?>
But, this didn´t help me at all;
So, anyone can help me????
I discovered in another Forum how to solve it;
I have saved my file in a different type of file.
It should be just UTF-8 and I saved something else;
So, just save it as UTF-8 your PHP File;
Related
I have a php file that contains:
<?php
$no = $_POST['no'];
$url = "http://domain.tld/answer.xml";
?>
The $url stores a URL that links to an xml file and that xml files is like:
<?xml version="1.0" encoding="UTF-8"?>
<vxml version = "2.1" >
<Call> </Call>
</vxml>
I want to pass the $_POST['no'] to my xml file. Suppose if $_POST['no'] is 9999988888, then the tag in xml should be like:
<Call>9999988888</Call>
Can anyone tell how to do this? :)
that xml file is on my own server.
you could use simplexml, as:
$no = $_POST['no'];
$xml = simplexml_load_file('your_xml_file.xml');
$xml->Call = $no;
//save/update your xml
$xml->asXML('your_xml_file.xml');
You need to generate the xml using PHP.
First of all, rename your .xml to .php.
Then, start it using
<?php
header('Content-type: text/xml');
?>
When you reach your call tag, change it to :
<Call><?php echo intval($_GET['no']); ?></Call>
Note: I've assumed your no is a number. If it's not, remove intval. It's nice to have it as it will protect you from injections.
At last, call your XML file using :
$url = "http://domain.tld/answer.php?no=8877454";
Your XML should have some keywords, i.e.
<?xml version="1.0" encoding="UTF-8"?>
<vxml version = "2.1" >
<Call>{{CALL}}</Call>
</vxml>
In your PHP File, just fetch this XML & use str_replace to replace those keywords,
<?php
$no = $_POST['no'];
$url = "http://domain.tld/answer.xml";
$xml = file_get_contents($url);
$newXML = str_replace("{{CALL}}", $no, $xml);
header('Content-type: text/xml');
echo $newXML;
?>
I am trying to develop my own XML RSS feed based on PHP output from MySQL queries. However I keep getting "entity X not defined" error messages for all the ASCII characters in my DB content fields, even though I have set everything to UTF8 encoding and charset (database connection, xml version, utf8_encode), but nothing removes the error:
<?php
$connection = mysqli_connect( .... )
$connection->set_charset("utf8");
header("Content-type: text/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<rss version="2.0">';
$query = mysqli_query($connection,"SELECT * FROM news ORDER BY pubdate DESC LIMIT 10");
while($row = mysqli_fetch_assoc($query)){
$title = utf8_encode($row['title']);
$content = utf8_encode($row['content']);
echo '<item><title>'.$title.'</title>';
echo '<description>'.$content.'</description></item>';
} // end while
echo '</channel>';
echo '</rss>';
?>
What am I missing?
Thanks a lot!
I also faced the same issue and fixed using below link
I think you are looking for this
http://help.simplytestable.com/errors/html-validation/general-entity-x-not-defined-and-no-default-entity/
You will have to escape the $title and $content variables. Check htmlspecialchars().
For a better solution, use DOM to create the the XML. This will make sure that you create a valid XML.
I have to following code to upload data to a third party. However when I test it in a validator I get the following error:
Undefined root elements:items and text/xml media type is not specific enough.
I have no clue has what I need to change.
Here is the code:
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>
<items>';
mysql_select_db($database_dconn, $dconn);
$query_feed = "SELECT * FROM dat_table WHERE time BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()";
$feed = mysql_query($query_feed, $dconn) or die(mysql_error());
while ($row_feed = mysql_fetch_assoc($feed)){
echo'<item>
<name>'.$row_feed['Name'].'</name>
<email>'.$row_feed['email'].'</email>
<date>'.$row_feed['Date'].'</date>
<description>'.$row_feed['Make'].' '.$row_feed['Model'].' '.$row_feed['Type'].'</description>
<logon>'.$row_feed['Logon'].'</logon>
<category>'.$row_feed['Type'].'/'.$row_feed['Make'].'</category>
<product_search_code>'.$row_feed['Product_search_code'].'</product_search_code>
<order_ref>'.$row_feed['Invoice'].'</order_ref>
<product_link>'.$row_feed['Product_link'].'</product_link>
<customer_ref>'.$row_feed['Invoice'].'</customer_ref>
<amount>'.$row_feed['Price'].'</amount>
<currency>GBP</currency>
</item>';
}
echo '</items>
';
This is RSS out put in validator:
<?xml version="1.0" encoding="UTF-8"?>
<items><item>
<name>Name of customer</name>
<email>customer#gmail.com</email>
<date>03/04/2014</date>
<description>Roland FP80BK Piano</description>
<logon>www.pianoandkeyboardshoponline.co.uk</logon>
<category>Piano/Roland</category>
<product_search_code>264</product_search_code>
<order_ref>8336</order_ref>
<product_link>http://www.pianoandkeyboardshop.co.uk/Roland-FP80-Digital-Piano-in-Satin-Black/264</product_link>
<customer_ref>8336</customer_ref>
<amount>1500.00</amount>
<currency>GBP</currency>
</item><item>
next item etc
---
--
</item><items>
Any help is really appreciated.
The text/xml content type is for generic XML documents. For a feed, try using
header('Content-Type: application/rss+xml');
But anyway, in order to create a valid RSS you have to use the correct RSS tags (Something like this). You seem to use a lot of custom tags instead, so I don't know what exactly you need to do. The easiest solution would be to use namespaces for your custom tags, so something like:
header('Content-Type: application/rss+xml');
echo '<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:mytags="http://mytags.something.net">
<channel>
<title>My RSS Title</title>
<description>This is the description for my RSS feed</description>
<link>http://www.someexamplerssdomain.com/main.html</link>
<mytags:items>';
mysql_select_db($database_dconn, $dconn);
$query_feed = "SELECT * FROM dat_table WHERE time BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()";
$feed = mysql_query($query_feed, $dconn) or die(mysql_error());
while ($row_feed = mysql_fetch_assoc($feed)){
echo '<mytags:item>
<mytags:name>'.$row_feed['Name'].'</mytags:name>
<mytags:email>'.$row_feed['email'].'</mytags:email>
<mytags:date>'.$row_feed['Date'].'</mytags:date>
<mytags:description>'.$row_feed['Make'].' '.$row_feed['Model'].' '.$row_feed['Type'].'</mytags:description>
<mytags:logon>'.$row_feed['Logon'].'</mytags:logon>
<mytags:category>'.$row_feed['Type'].'/'.$row_feed['Make'].'</mytags:category>
<mytags:product_search_code>'.$row_feed['Product_search_code'].'</mytags:product_search_code>
<mytags:order_ref>'.$row_feed['Invoice'].'</mytags:order_ref>
<mytags:product_link>'.$row_feed['Product_link'].'</mytags:product_link>
<mytags:customer_ref>'.$row_feed['Invoice'].'</mytags:customer_ref>
<mytags:amount>'.$row_feed['Price'].'</mytags:amount>
<mytags:currency>GBP</mytags:currency>
</mytags:item>';
}
echo '</mytags:items>
</channel>
</rss>';
You have no xmlns (XML name space) defined. Thus the error is probably due to the fact that the root element items is... not defined, as said in the error message. See this link or that link for explanations on xmlns.
First, to complete the previous answer, after the first echo, add a rss-specific line: echo '<rss version="2.0">';
Second, you either need to add an xmlns definition in the above line echo '<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">'; for example, or to change the root element name. try with channel or feed. Better yet, find a feed which is read by the service you want your feed to be read by, and have a look at the beginning of the file; look for xmlns and root element names.
best,
I have a slight problem. I need to parse a file and populate a web banner with the results. Problem is, the file is called : "_banner_products.php" and it's contents are as follows:
<?php header('Content-Type:text/xml'); ?><?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<carouselle>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
<firstLayer>
.....
.....
</firstLayer>
</carouselle>
How can I loop through this file to group all the "firstLayer" children into one and so on..
If I just use:
$file = fopen("_banner_products.php", "r");
while (!feof($file)){
$line = fgets($file);
}
simplexml_load_file throws this-
"_banner_products.php:1: parser error : Start tag expected, '<' "
Then I only get the contents of the <...> tags meaning there is no way for me to differentiate if I am out of the scope already.
Thanks for anyone responding. If anything is unclear I´ll try to explain more.
EDIT.
Thank you for the solution, indeed using the full URL worked:
simplexml_load_file("http://localhost/MySite/_banner_products.php");
You are having issue because simplexml_load_file is treating your file like a local xml file .. what you need to do is add the full URL
Example
simplexml_load_file("http://localhost/web/_banner_products.php");
Use Case getting layerName for example
_banner_products.php
<?php
header ( 'Content-Type:text/xml' );
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<carouselle>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
</carouselle>
view details
$xml = simplexml_load_file("http://localhost/lab/stockoverflow/_banner_products.php");
echo "<pre>" ;
foreach($xml as $key => $element)
{
echo $element->layerName , PHP_EOL ;
}
The most obvious way to do this is to strip out the first line, and add the XML declaration back in with your code.
You could also parse the file with PHP, using eval(), but be very sure about what you are parsing, as this could be a very large security hole.
I'm trying to create a sitemap that will automatically update. I've done something similiar with my RSS feed, but this sitemap refuses to work. You can view it live at http://designdeluge.com/sitemap.xml I think the main problem is that its not recognizing the PHP code. Here's the full source:
<?php
include 'includes/connection.php';
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<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://designdeluge.com/</loc>
<lastmod>2010-04-20</lastmod>
<changefreq>weekly</changefreq>
<priority>1.00</priority>
</url>
<url>
<loc>http://designdeluge.com/about.php</loc>
<lastmod>2010-04-20</lastmod>
<changefreq>never</changefreq>
<priority>0.5</priority>
</url>
<?php
$entries = mysql_query("SELECT * FROM Entries");
while($row = mysql_fetch_assoc($entries)) {
$title = stripslashes($row['title']);
$date = date("Y-m-d", strtotime($row['timestamp']));
echo "
<url>
<loc>http://designdeluge.com/".$title."</loc>
<lastmod>".$date."</lastmod>
<changefreq>never</changefreq>
<priority>0.8</priority>
</url>";
} ?>
</urlset>
The problem is that the dynamic URL's (e.g. the ones pulled from the DB) aren't being generated and the sitemap won't validate. Thanks!
EDIT: Right now, I'm just trying to get the code itself working. I have it set up as a PHP file on my local testing server. The code above is being used. Right now, nothing displays nothing on screen or in the source. I'm thinking I made a syntax error, but I can't find anything. Any and all help is appreciated!
EDIT 2: Ok, I got it sorted out guys. Apparently, I had to echo the xml declaration with PHP. The final code is posted above. Thanks for your help!
If you take a look at the sitemap.xml that's generated (using view source, in your browser, for example), you'll see this :
<?php header('Content-type: text/xml'); ?>
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http:/
...
The <?php, present in that output, shows that PHP code is not interpreted.
This is probably because your webserver doesn't recognize .xml as an extension of files that should contain PHP code.
At least two possible solutions :
Re-configure your server, so XML files go through the PHP interpreter (might not be such a good idea : that can cause problems with existing files ! )
Change the extension of your sitemap, to sitemap.php for example, so it's interpreted by your server.
I would add another solution :
Have a sitemap.php file, that contains the code
And use a RewriteRule so the sitemap.xml URL actually points to the sitemap.php file
With that, you'll have the sitemap.xml URL, which is nice (required ? ), but as the code will be in sitemap.php, it'll get interpreted.
See Apache's mod_rewrite.
The simplest solution is to add to your apache .htaccess file the following line after RewriteEngine On
RewriteRule ^sitemap\.xml$ sitemap.php [L]
and then simply having a file sitemap.php in your root folder that would be normally accessible via http://yoursite.com/sitemap.xml, the default URL where all search engines will firstly search.
The file sitemap.php shall start with
<?php header('Content-type: application/xml; charset=utf-8') ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>
I've used William's code (thanks) and with a few small modifications it worked for me.
I think the line:
header("Content-type: text/xml");
should be the second line after the top <?php
Incidentally, just a small point to anyone else that copies it, but there is a single space character before the <?php in the first line - if you inadvertantly copy it as I did, you will spend a bit of time trying to figure out why the code won't work for you!
I had to tweak the MySql select statement a little bit too.
Finally, in the output, I have used a variable $domain so that this piece of code can be used as a template without the need to think about it (provided you use the same table name each time). The variabe is added to the connectdb.php file which is included to connect to the database.
Here is my working version of the William's code:
<?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.DOMAIN.co.uk/</loc>
<priority>1.00</priority>
</url>
<?php
$sql = "SELECT * FROM pages WHERE onshow = 1 ORDER BY id ASC";
$result = mysql_query($sql,$conn);
while($row = mysql_fetch_array($result))
{
$filename = stripslashes($row['filename']);
?>
<url>
<loc>http://www.<?php echo "$domain"; ?>/<?php echo "$filename" ?></loc>
<changefreq>monthly</changefreq>
<priority>0.5</priority>
</url>
<?php } ?>
</urlset>
Here is the easiest way to creating and updating sitemap.xml file.
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
require_once('database.php');
$sitemapText = '<?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">
<url>
<loc>http://ajkhub.com/</loc>
<lastmod>2021-08-18T18:32:09+00:00</lastmod>
<priority>1.00</priority>
</url>
<url>
<loc>http://ajkhub.com/includes/about.php</loc>
<lastmod>2021-08-18T18:32:09+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>http://ajkhub.com/includes/privacy-policy.php</loc>
<lastmod>2021-08-18T18:32:09+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>http://ajkhub.com/includes/termsandcondition.php</loc>
<lastmod>2021-08-18T18:32:09+00:00</lastmod>
<priority>0.80</priority>
</url>';
$sql = "SELECT * FROM page ORDER BY id DESC LIMIT 4";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$sitemapText .= ' <url>
<loc>'.$actual_link."/".$row['page'].'</loc>
<lastmod>'.date(DATE_ATOM,time()).'</lastmod>
<priority>0.80</priority>
</url>';
}
}
$sitemapText .= '</urlset>';
$sitemap = fopen("sitemap.xml", "w") or die("Unable to open file!");
fwrite($sitemap, $sitemapText);
fclose($sitemap);