How to remove space from start of a php file? - php

Have a PHP file which is making RSS feeds it is giving error because of the space appending before the output XML file. I have checked the code and the variable containing the variable $s which is making XML file does not have any white space before it.
<?php
header("Content-Type: text/xml; encoding=UTF-8");
$s = "<?xml version='1.0' encoding='UTF-8'?><rss version='2.0'><channel>";
echo ltrim($s);
foreach($this->results as $row)
{
$title=$row->name;
$description=$row->description;
echo "<item><title>$title</title><link>$link</link><description>$description</description></item>";
}
echo "</channel></rss>";
?>

Make sure no white space before <?php
<?php
header("Content-Type: text/xml; encoding=UTF-8");
$s = "<?xml version='1.0' encoding='UTF-8'?><rss version='2.0'><channel>";
echo ltrim($s);
foreach($this->results as $row)
{
$title=$row->name;
$description=$row->description;
echo "<item><title>$title</title><link>$link</link><description>$description</description></item>";
}
echo "</channel></rss>";
?>

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

using <![CDATA in xml response

I have looked for an answer to this question but I have been unable to find an answer that works for my application.
I need to give a response from a sql query in xml format but the response is a url so i will need to use cdata. Here is my code.
if ($_POST['action']=="getSermon")
{
//echo"You got it";
$SQL = "SELECT * FROM sermons";
$allsermon = mysql_query($SQL);
$sermonrow = mysql_fetch_assoc($allsermon);
header('Content-type: text/xml; charset=utf-8');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
$xml = '<?xml version="1.0" encoding="utf-8"?>';
echo $xml;
echo "<SERMON_INFO>";
do {
echo "<SERMON>";
echo "<ID>" . $sermonrow['id'] . "</ID>";
echo "<NAME>" . $sermonrow['Name'] . "</NAME>";
echo "<URL>".<![CDATA[$sermonrow['url']]>."</NAME>";
//echo "<URL>". $sermonrow['url'] . "</URL>";
echo "</SERMON>";
mysql_free_result($result);
} while ($sermonrow = mysql_fetch_assoc($allsermon));
mysql_free_result($allsermon);
echo "</SERMON_INFO>";
mysql_close($db);
}
when i do this i get an error 500 and the page will not load.
The correct syntax is
echo "<URL><![CDATA[".$sermonrow['url']."]]></URL>";
But I'd rather you use an XML library like DOMDocument or SimpleXML to build your document, more complex, but you'll be less prone to simple errors like mixing up a closing tag name (see </NAME>).

phpQuery: Replace all occurrences of text with another

I am trying to parse a website homepage to convert it into xml file to be used as an api in my app.
So far I have successfully done so. However, the parsed text contains the & (ampersand) character which causes the XML parser to fail.
I am looking for a solution that doesn't use the CDATA or doesn't output CDATA in the XML file.
I want to replace & with and at every occurrence. What phpQuery method should I use?
This causes error in browser because the text() method returns a text with
& character in it.
require('phpQuery/phpQuery.php');
$all=phpQuery::newDocumentFileHTML('BPUT.htm', $charset = 'utf-8');
$links = $all['a.myblue'];
echo '<notice>';
foreach ($links as $link) {
echo '<text>';
echo pq($link)->text();
echo '</text>';
echo '<url>';
echo pq($link)->attr('href');
echo '</url>';
}
echo '</notice>';
?>
I do not want to use CDATA, as the CDATA tag is visible in the generated XML :
<?php
header('Content-type: text/xml');
require('phpQuery/phpQuery.php');
$all=phpQuery::newDocumentFileHTML('BPUT.htm', $charset = 'utf-8');
$links = $all['a.myblue'];
echo '<notice>';
foreach ($links as $link) {
echo '<text>';
echo "<![CDATA[";
echo pq($link)->text();
echo "]]>";
echo '</text>';
echo '<url>';
echo pq($link)->attr('href');
echo '</url>';
}
echo '</notice>';
?>
bumping for answers.

How to echo <?xml version="1.0" encoding="utf-8"?> in a page?

I'm using a script to display a page in XML, but I need it to display at start but whenever I try, it breaks the code.
Heres the code I tried:
<?php
header('Content-Type: application/xml');
$output = "
<?xml version='1.0' encoding='utf-8' ?>
";
print ($output);
?>
but its not working, any help ?
use echo , Basic Simple XML
<?php
header('Content-Type: application/xml');
$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo $output;
?>
yes, use echo, but you don't need additional variable and you can use single quote string without backslash...
<?php
header('Content-Type: application/xml');
echo '<?xml version="1.0" encoding="UTF-8"?' . ">\n"; //connate strings to protect end of php formatting in simple editors, and use new line tag
?>

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 ?> .

Categories