PDA

View Full Version : Parse error: parse error, unexpected T_STRING


harlequin2k5
10-02-2007, 12:24 AM
I went through a few different tutorials for creating a dynamic rss feed and I found this one by tiffany brown (http://www.tiffanybbrown.com/2005/12/22/dynamic-rss-feeds-using-php-mysql-and-apache) to be most helpful

unfortunately I'm doing something wrong and not being well-versed with xml at all I'm not sure what to do next

I get that nasty lil parse error and considering that the first line is
<?xml version="1.0" encoding="utf-8"?>
I didn't know what to do first

this is my feed - there are currently 3 articles that it can display:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Shopping Center Advisor</title>
<link>http://www.shoppingcenter-advisor.com/test</link>
<description>The definitive source for Shopping Center Information</description>
<language>en-us</language>
<managingEditor>webmaster@shoppingcenter-advisor.com</managingEditor>
<webMaster>webmaster@shoppingcenter-advisor.com</webMaster>
<copyright>2007 Ronald L. McDonald</copyright>
<?
include('functionlist.php');
$conn=doconnect();
$query = "SELECT id,title,content,DATE_FORMAT(added,'%c/%e/%y') as added FROM articles order by added desc limit 5";
$result = mysql_query($query) or die("Error: " . mysql_error());
while ($row = mysql_fetch_array($result))
{
?>
<item>
<title> <?=htmlentities(strip_tags($row['title'])); ?></title>
<description> <?=htmlentities(strip_tags($row['content'],'ENT_QUOTES'));?></description>
<link>http://www.shoppingcenter-advisor.com/test/articles.php?p=<?=$row['id'];?></link>
</item>
<? } ?>
</channel>
</rss>

Alex Vincent
10-02-2007, 01:53 AM
I'll bet five pennies that it's because PHP is trying to interpret the XML declaration. <?php ?> is what PHP should use to start reading text, but <?foo ?>, <?bar ?>, <?baz ?>, <?etc ?>, are all often caught by PHP as well. So's <?xml ?> and <?xml-stylesheet ?>. It's a pet peeve of mine.

This is because of PHP's default of short open tags. See http://www.php.net/manual/en/ini.core.php for details. You probably need to turn them off in your Apache configuration (perhaps by .htaccess).

CFMaBiSmAd
10-02-2007, 02:10 AM
The short open tag parsing is not smart enough to know that -
<?any_thing_else_here (that is not "php", "=", white-space, or a newline) is NOT an opening php tag.

Either put the <?xml... tag in a string and echo it, escape the ? (I think <\?xml... will work), or turn off short open tags.

harlequin2k5
10-02-2007, 03:28 AM
it still took a bit of work and it's not validating (http://feedvalidator.org/check.cgi?url=http%3A%2F%2Fwww.shoppingcenter-advisor.com%2Ftest%2Ffeed.xml) - but the feed is finally working!!!

this is what I wound up with...
<?php echo '<?xml version="1.0" encoding="iso-8859-1"?>'; ?>
<rss version="2.0">
<channel>
<title>Shopping Center Advisor</title>
<link>http://www.shoppingcenter-advisor.com/test</link>
<description>The definitive source for Shopping Center Information</description>
<language>en-us</language>
<managingEditor>webmaster@shoppingcenter-advisor.com</managingEditor>
<webMaster>webmaster@shoppingcenter-advisor.com</webMaster>
<copyright>2007 Ronald L. McDonald</copyright>
<?php
include('functionlist.php');
$conn=doconnect();
$query = "SELECT id,title,content,DATE_FORMAT(added,'%c/%e/%y') as added FROM articles order by added desc limit 5";
$result = mysql_query($query) or die("Error: " . mysql_error());
while ($row = mysql_fetch_array($result))
{
?>
<item>
<title> <?=htmlentities(strip_tags($row['title'])); ?></title>
<description> <?=htmlentities(strip_tags($row['content'],'ENT_QUOTES'));?></description>
<link>http://www.shoppingcenter-advisor.com/test/feed.php?p=<?=$row['id'];?></link>
</item>
<? } ?>
</channel>
</rss>

harlequin2k5
10-02-2007, 04:12 AM
is that because it didn't validate? is it one of those errors causing it to not work?

and unfortunately I also need to test in aol and it's not working there either...

_Aerospace_Eng_
10-02-2007, 04:34 PM
Its not validating because its not parsing the php. You need to change it from .xml to .php and you also need to pass the right headers. I've taken out the while loop but you should get the idea. You also need a <guid></guid> (http://feedvalidator.org/docs/warning/MissingGuid.html) (click me) on the <item></item>.
<?php
header("Content-Type: application/xml");
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss version="2.0">
<channel>
<title>Shopping Center Advisor</title>
<link>http://www.shoppingcenter-advisor.com/test
</link>
<description>The definitive source for Shopping Center Information</description>
<language>en-us</language>
<managingEditor>webmaster@shoppingcenter-advisor.com</managingEditor>
<webMaster>webmaster@shoppingcenter-advisor.com</webMaster>
<copyright>2007 Ronald L. McDonald</copyright>
<item>
<title>Test</title>
<description>This is a description</description>
<guid>http://google.com</guid>
<link>
http://google.com
</link>
</item>
</channel>
</rss>
http://feedvalidator.org/check.cgi?url=http%3A%2F%2Fprod.freehostia.com%2Ftest.php