Extra content at the end of the document on chrome - php

http://seosailor.com/beta2/articles/showrss this url give error Extra content at the end of the document on chrome
my code is this
function showrss() {
header("Content-Type: application/xml; charset=ISO-8859-1");
$query_items = "select * from articles";
$result_items = mysql_query ($query_items) or die("Some error: ".mysql_error());
$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?><rss version="2.0"><channel>';
while($row = mysql_fetch_array($result_items))
{
//$des = mysql_real_escape_string($row['a_description']);
// $a_des = str_replace(']]>', ']]>',$row['a_description']);
//$a_des = strip_tags($row['a_description']);
// $a_des = preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags($row['a_description']));
$a_des = htmlspecialchars($row['a_description']);
$xml .= '<item>
<title>'.$row["a_title"].'</title>
<link>'.$row["a_url"].'</link>
<description>'.$a_des.'</description></item>';
} $xml .= '</channel>';
$xml .= '</rss></xml>';
echo $xml;}

That's because of the extra </xml> closing tag at the end (there is no opening <xml> tag).

Related

Compress sitemap.xml into sitemap.xml.gz in php making the file damage

I am trying to convert the sitemap.xml into sitemap.xml.gz , The pages I am extracting from the database , it is converting into sitemap.xml , but when I am trying to compress this is not working . this is my code it is making the file damage .
header('content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>';
$xmlString .= '<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">';
include("admin/functions/dbconfig.php");
$sql = "select * from zz where aa between 1 and 30000";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($result)){
$url = $row["aa"];
$xmlString .= '<url>';
$xmlString .= '<loc>http://mynewdomain.com/page.php?word='.htmlentities($url).'</loc>';
$xmlString .= '<lastmod>'.date("Y-m-d").'</lastmod>';
$xmlString .= '<changefreq>monthly</changefreq>';
$xmlString .= '<priority>0.5</priority>';
$xmlString .= '</url>';
}
$xmlString .= '</urlset>';
gzwrite("compress", gzencode($xmlString));
gzclose("compress");

updating xml file using php

hey there i have an xml file that has what i need to do is to add an event with same location name A
<timetable>
<location name="A" >
<event >
<title>EVENT TITLE </title>
<subtitle>Amman -text </subtitle>
<description>Amman -text </description>
</event>
</location>
</timetable>
so it can be like this
<timetable>
<location name="A" >
<event >
<title>EVENT TITLE </title>
<subtitle>Amman -text </subtitle>
<description>Amman -text </description>
</event>
<event >
<title>EVENT TITLE </title>
<subtitle>Amman -text </subtitle>
<description>Amman -text </description>
</event>
</location>
</timetable>
i got stuck in my php code where i want to cheack if the event is created or not if its created modify my xml with name of the event and add the new one
this is my full php inserting code `
if(isset($_GET['coname'])){
$coid = $_GET['id'];
$cname=$_GET['coname'];
$title = $_POST ['title'];
$sub = $_POST ['sub'];
$description = $_POST ['description'];
$location = $_POST ['location'];
$event = $_POST ['event'] ;
$str =$_POST ['str'] ;
$end =$_POST ['end'] ;
$topic = $_POST ['topic'] ;
$sql="INSERT INTO timeline (title,sub,description,location,event,str,end,topic,coid)
VALUES
('$title','$sub','$location','$location','$event','$str','$end','$topic','$coid')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
$q = mysqli_query($con,"SELECT * FROM timeline where coid = $coid") or die(mysqli_error());
$xml = '<timetable start="'.$st.'" end="'.$en.'" interval="'.$in.'" title="'.$da.'">';
while($r = mysqli_fetch_array($q)){
$loc=$r['topic'];
$evns=$r['str'];
$evne= $r['end'];
$xml .= '<location name="'.$loc.'" subtext=" ">';
$xml .= '<event start="'.$evns.'" end="'.$evne.'">';
$xml .= "<title>".$r['title']."</title>";
$xml .= "<subtitle>".$r['location']."</subtitle>";
$xml .= "<description>".$r['description']."</description>";
$xml .= "</event>";
$xml .= "</location>";
}
$xml .= "</timetable>";
$sxe = new SimpleXMLElement($xml);
$sxe->asXML('xml/'.$cname.'.xml'); `
Amer, rather than creating the XML from strings, I'd use the simplexml methods:
Inserting a new <event> in existing XML:
$xml = simplexml_load_string($x); // assume XML in $x
$loc = $xml->xpath("location[#name = 'A']")[0]; // select <location> with name = A
$event = $loc->addChild("event");
$event->addAttribute("start", "2013-05-20 10:00:00");
$event->addAttribute("end", "2013-05-20 14:30:00");
$event->addChild("title", "some title");
$event->addChild("subtitle", "some subtitle");
$event->addChild("description", "some description");
see it working: http://codepad.viper-7.com/12xtVD
From what I understand, you want to create a child element. This is how I would go about doing this:
PHP:
$eleone=$_POST['elementone'];
$eletwo=$_POST['elementtwo'];
$file = "verbs.xml";
$openf = fopen($file, "c+") or die ("Cannot open file");
$str = fread ($openf, filesize($file));
$xml = new DOMDocument('1.0', 'iso-8859-1');
$xml->formatOutput=TRUE;
$xml->preserveWhiteSpace = FALSE;
$xml ->loadXML($openf) or die ("There has been an error with opening the XML file. Our team has been notified and will start working to fix this problem.");
//this is the original document
echo "<xmp>OLD:\n". $xml->saveXML(). "<xmp>";
//this is how you get the document element
$root= $xml ->documentElement;
$firstnode= $root->firstChild;
//good stuff right here; how to make a node
$ori= $firstnode->childNodes->item(2);
$eleadd= $xml->createElement($elementone);
$eleaddt= $xml->createTextNode($//what gets shown in $eleadd );
$eleadd->appendChild("$idt");
If you don't want all of this, you may be able to delete some non-crucial things like the parent elements. If you need more information, http://www.phpeveryday.com/articles/PHP-XML-Adding-XML-Nodes-P414.html is the place to go, or where I found my information.

how to create xml files via php and MySQL [duplicate]

This question already has answers here:
How to generate XML file dynamically using PHP?
(8 answers)
Closed 8 years ago.
I have a problem in XML files.
I searched through the internet and found lots of examples for my problem but I am not an expert on XML files and couldn't solve my problem. I want to do and XML file and work like RSS FEED. So, I am taking my data from my database and try to create the xml-code. Here what I have in my php file
(and it is not validated because of this problem: Undefined root element: channel)
<?php
include "connection.php";
//create the table with the fields
$rss_table = array();
$query = mysql_query("SELECT * FROM rssfeeds");
while($values_query = mysql_fetch_assoc($query))
{
$rss_table [] = array(
'title' => $values_query['title'],
'description' => $values_query['summary'],
'link' => $values_query['link']
);
}
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->encoding = "utf-8";
$r = $doc->createElement( "channel" );
$doc->appendChild( $r );
//$i=0;
foreach( $rss_table as $rss )
{
$b = $doc->createElement( "item" );
$title = $doc->createElement( "title" );
$title->appendChild(
$doc->createTextNode( $rss['title'] )
);
$b->appendChild( $title );
$description = $doc->createElement( "description" );
$description->appendChild(
$doc->createTextNode( $rss['description'] )
);
$b->appendChild( $description );
$link = $doc->createElement( "link" );
$link->appendChild(
$doc->createTextNode( $rss['link'] )
);
$b->appendChild( $link );
$r->appendChild( $b );
}
echo $doc->saveXML();
$doc->save("rssfeeds.xml")
?>
I want to have title - link - description
Simple one... nothing more
And here is what I get in rssfeeds.xml file:
<?xml version="1.0" encoding="utf-8"?>
<channel>
<item>
<title>winter week</title>
<description>You can come as you are! </description>
<link>http://tdm2000international.org/tdm2000international/news.php</link>
</item>
<item>
<title>Greek night</title>
<description>elliniki bradua sto magazi</description>
<link>http://tdm2000international.org/tdm2000international/news.php</link>
</item>
<item>
<title>event website</title>
<description>first of december, how is it going?</description>
<link>http://tdm2000international.org/tdm2000international/news.php</link>
</item>
</channel>
Nice format, but it has problem. I do not understand where the problem is.
Any help would be appreciated
(I also check this website for any solution, but I could not found my solution..So, sorry about this post, if it is already exist)
ok I found my one way .. I did it with FILES via php: this is the code if anyone needs help to that:
<?php
include "connection.php";
$myFile = "rss.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$rss_txt .= '<?xml version="1.0" encoding="utf-8"?>';
$rss_txt .= "<rss version='2.0'>";
$rss_txt .= '<channel>';
$query = mysql_query("SELECT * FROM rssfeeds");
while($values_query = mysql_fetch_assoc($query))
{
$rss_txt .= '<item>';
$rss_txt .= '<title>' .$values_query['title']. '</title>';
$rss_txt .= '<link>' .$values_query['link']. '</link>';
$rss_txt .= '<description>' .$values_query['summary']. '</description>';
$rss_txt .= '</item>';
}
$rss_txt .= '</channel>';
$rss_txt .= '</rss>';
fwrite($fh, $rss_txt);
fclose($fh);
?>

how to compress a sitemap with php

I have this below code and it work fine
header ("content-type: text/xml");
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= '<urlset xmlns="http://www.google.com/schemas/sitepam/0.84">';
$xml .= '<url><loc>'.SiteRoot.'</loc><changefreq>daily</changefreq><priority>1.0</priority></url>';
$xml .= '<url><loc>'.SiteRoot.'/directory</loc><changefreq>daily</changefreq><priority>0.9</priority></url>';
$Query = mysql_query ("SELECT link FROM `om` ORDER BY `link`");
while($row = mysql_fetch_array($Query)) {
$xml .= '<url>';
$xml .= '<loc>'.GenerateLink( 'link',$row['link'] ).'</loc>';
$xml .= '<changefreq>weekly</changefreq>';
$xml .= '<priority>0.8</priority>';
$xml .= '</url>';
}
$xml .= '</urlset>';
echo $xml;
When i try to compress it with mime header
header('content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
Browser download a .gz file but it's not open. winrar give me a error that said: The archive is either in unknown format or damaged
This is the final code :
// header ("content-type: text/xml");
header('content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= '<urlset xmlns="http://www.google.com/schemas/sitepam/0.84">';
$xml .= '<url><loc>'.SiteRoot.'</loc><changefreq>daily</changefreq><priority>1.0</priority></url>';
$xml .= '<url><loc>'.SiteRoot.'/directory</loc><changefreq>daily</changefreq><priority>0.9</priority></url>';
$Query = mysql_query ("SELECT link FROM `om` ORDER BY `link`");
while($row = mysql_fetch_array($Query)) {
$xml .= '<url>';
$xml .= '<loc>'.GenerateLink( 'link',$row['link'] ).'</loc>';
$xml .= '<changefreq>weekly</changefreq>';
$xml .= '<priority>0.8</priority>';
$xml .= '</url>';
}
$xml .= '</urlset>';
echo $xml;
Try using some of the built in gzip functions like gzencode
echo gzencode($xml);

php XML DOM translates special chars to &#xYY;

I send this with AJAX POST:
<li><ul class "zone zCentral ui-sortable"><li><ul class="region rCol3 ui-sortable"><li class="" style=""><div><span class="tc tc_video">574081</span> <span>video: 'Mundo.Hoy': ¿Dónde habré olvidado... mi memoria?</span></div></li></ul></li></ul></li>
I do this to create XML:
header('Content-type: text/html; charset=utf-8');
if(isset($_POST) && isset($_POST['data']))
{
$data = '<ul id="zone_container" class="ui-sortable">';
$data .= $_POST['data'];
$data .= '</ul>';
$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadXML($data);
echo $dom->saveXML();
exit();
}
and i get this:
<?xml version="1.0"?>
<ul id="zone_container" class="ui-sortable">
<li><ul class="zone zCentral ui-sortable"><li><ul class="region rCol3 ui-sortable"><li class="" style=""><div><span class="tc tc_video">574081</span> <span>video: 'Mundo.Hoy': ¿Dónde habré olvidado... mi memoria?</span></div> </li></ul></li></ul></li></ul>
¿Dónde habré olvidado... mi memoria?
translates to:
¿Dónde habr&#xE9 ; olvidado... mi memoria?
I need original chars in the XML, these are utf-8 valid and i don't know the reason for this encode :(
The easiest way to fix this is to set the encoding type after you have loaded the XML:
$dom = new DOMDocument();
$dom->loadXML($data);
$dom->encoding = 'utf-8';
echo $dom->saveXML();
exit();
You can also fix it by putting an XML declaration at the beginning of your data:
$data = '<?xml version="1.0" encoding="utf-8"?>' . $data;
$dom = new DOMDocument();
$dom->loadXML($data);
echo $dom->saveXML();
exit();
I solved with this:
header('Content-type: text/html; charset=utf-8');
if(isset($_POST) && isset($_POST['data']))
{
$data = '<?xml version="1.0" encoding="utf-8"?>';
$data .= '<ul id="zone_container" class="ui-sortable">';
$data .= $_POST['data'];
$data .= '</ul>';
$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadXML($data);
echo $dom->saveXML();
exit();
adding the:
$data = '<?xml version="1.0" encoding="utf-8"?>';
to the XML at the beginning
thanks for responses :)

Categories