Dynadot โ€” .com Registration $8.99

Parse error: syntax error, unexpected T_STRING

Spaceship Spaceship
Watch
Impact
0
Hi Guys.

Im a complete beginner in programming, and was given a link to http://www.globalsyndication.com/rss-parser to embed RSS Latest News in my site.

However when I copy and paste the code into my .php file I get an error "

"Parse error: syntax error, unexpected T_STRING in /home/.bess/adnan278/www.golfchum.com/test.php on line 110."


Anyone help? Here is the code:

PHP:
<?php

/*
Created by Global Syndication's RSS Parser
http://www.globalsyndication.com/rss-parser
*/

set_time_limit(0);

$file = "http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/golf/rss.xml#";

$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;

function startElement($parser, $name, $attrs) {
   	global $rss_channel, $currently_writing, $main;
   	switch($name) {
   		case "RSS":
   		case "RDF:RDF":
   		case "ITEMS":
   			$currently_writing = "";
   			break;
   		case "CHANNEL":
   			$main = "CHANNEL";
   			break;
   		case "IMAGE":
   			$main = "IMAGE";
   			$rss_channel["IMAGE"] = array();
   			break;
   		case "ITEM":
   			$main = "ITEMS";
   			break;
   		default:
   			$currently_writing = $name;
   			break;
   	}
}

function endElement($parser, $name) {
   	global $rss_channel, $currently_writing, $item_counter;
   	$currently_writing = "";
   	if ($name == "ITEM") {
   		$item_counter++;
   	}
}

function characterData($parser, $data) {
	global $rss_channel, $currently_writing, $main, $item_counter;
	if ($currently_writing != "") {
		switch($main) {
			case "CHANNEL":
				if (isset($rss_channel[$currently_writing])) {
					$rss_channel[$currently_writing] .= $data;
				} else {
					$rss_channel[$currently_writing] = $data;
				}
				break;
			case "IMAGE":
				if (isset($rss_channel[$main][$currently_writing])) {
					$rss_channel[$main][$currently_writing] .= $data;
				} else {
					$rss_channel[$main][$currently_writing] = $data;
				}
				break;
			case "ITEMS":
				if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
					$rss_channel[$main][$item_counter][$currently_writing] .= $data;
				} else {
					$rss_channel[$main][$item_counter][$currently_writing] = $data;
				}
				break;
		}
	}
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
	die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
	if (!xml_parse($xml_parser, $data, feof($fp))) {
		die(sprintf("XML error: %s at line %d",
					xml_error_string(xml_get_error_code($xml_parser)),
					xml_get_current_line_number($xml_parser)));
	}
}
xml_parser_free($xml_parser);

// output HTML
 print ("<div class=\"channelname\">" . $rss_channel["TITLE"] . "</div>"); 
 print ("<div class=\"channeldescription\">" . $rss_channel["DESCRIPTION"] . "</div><br />"); 
if (isset($rss_channel["ITEMS"])) {
	if (count($rss_channel["ITEMS"]) > 0) {
		for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
			if (isset($rss_channel["ITEMS"][$i]["LINK"])) {
			print ("\n<div class=\"itemtitle\"><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>");
			} else {
			print ("\n<div class=\"itemtitle\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</div>");
			}
			 print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div><br />"); 		}
	} else {
		print ("<b>There are no articles in this feed.</b>");
	}
}
print ("<span style="font-size:xx-small;"><a href=\"http://www.globalsyndication.com/rss-parser\" style=\"color:white;\">Free PHP RSS Parser</a> - <a href=\"http://www.globalsyndication.com/rss-hosting\" style=\"color:white\">RSS Newsfeed Hosting</a></span>");
?>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Looks like you have odd numbers of brackets () in some places...you need to check your brackets, you cant have something like this:

if(!($foo) = (!($bar)))))

Which seems to be the case on the above code.
 
0
•••
xml_get_current_line_number($xml_parser)));

Seems to be oen of the problems.

Actually, the problem that is being reported (Line 110) is this

PHP:
print ("<span style="font-size:xx-small;"><a href=\"http://www.globalsyndication.com/rss-parser\" style=\"color:white;\">Free PHP RSS Parser</a> - <a href=\"http://www.globalsyndication.com/rss-hosting\" style=\"color:white\">RSS Newsfeed Hosting</a></span>");

Change it to:

PHP:
print ("<span style=\"font-size:xx-small;\"><a href=\"http://www.globalsyndication.com/rss-parser\" style=\"color:white;\">Free PHP RSS Parser</a> - <a href=\"http://www.globalsyndication.com/rss-hosting\" style=\"color:white\">RSS Newsfeed Hosting</a></span>");
 
0
•••
Thanks so far guys,

Well I went through and checked all the brackets to make sure they paired up, so that seems to be fixed (thanks rmwebs).

I also changed the 2 things you mentioned Tree so thankyou for pointing that out.

I uploaded the file and now it comes up with

"Parse error: syntax error, unexpected T_STRING in /home/.bess/adnan278/www.golfchum.com/test.php on line 88"

Here is the code as it stands, so further help would be much appreciated:

PHP:
<?php

/*
Created by Global Syndication's RSS Parser
http://www.globalsyndication.com/rss-parser
*/

set_time_limit(0);

$file = "http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/golf/rss.xml#";

$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;

function startElement($parser, $name, $attrs) {
   	global $rss_channel, $currently_writing, $main;
   	switch($name) {
   		case "RSS":
   		case "RDF:RDF":
   		case "ITEMS":
   			$currently_writing = "";
   			break;
   		case "CHANNEL":
   			$main = "CHANNEL";
   			break;
   		case "IMAGE":
   			$main = "IMAGE";
   			$rss_channel["IMAGE"] = array();
   			break;
   		case "ITEM":
   			$main = "ITEMS";
   			break;
   		default:
   			$currently_writing = $name;
   			break;
   	}
}

function endElement($parser, $name) {
   	global $rss_channel, $currently_writing, $item_counter;
   	$currently_writing = "";
   	if ($name == "ITEM") {
   		$item_counter++;
   	}
}

function characterData($parser, $data) {
	global $rss_channel, $currently_writing, $main, $item_counter;
	if ($currently_writing != "") {
		switch($main) {
			case "CHANNEL":
				if (isset($rss_channel[$currently_writing])) {
					$rss_channel[$currently_writing] .= $data;
				} else {
					$rss_channel[$currently_writing] = $data;
				}
				break;
			case "IMAGE":
				if (isset($rss_channel[$main][$currently_writing])) {
					$rss_channel[$main][$currently_writing] .= $data;
				} else {
					$rss_channel[$main][$currently_writing] = $data;
				}
				break;
			case "ITEMS":
				if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
					$rss_channel[$main][$item_counter][$currently_writing] .= $data;
				} else {
					$rss_channel[$main][$item_counter][$currently_writing] = $data;
				}
				break;
		}
	}
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
	die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
	if (!xml_parse($xml_parser, $data, feof($fp))) {
		die(sprintf("XML error: %s at line %d"))
					xml_error_string(xml_get_error_code($xml_parser)),
					xml_get_current_line_number($xml_parser),
xml_parser_free($xml_parser);

// output HTML
 print ("<div class=\"channelname\">" . $rss_channel["TITLE"] . "</div>"); 
 print ("<div class=\"channeldescription\">" . $rss_channel["DESCRIPTION"] . "</div><br />"); 
if (isset($rss_channel["ITEMS"])) {
	if (count($rss_channel["ITEMS"]) > 0) {
		for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
			if (isset($rss_channel["ITEMS"][$i]["LINK"])) {
			print ("\n<div class=\"itemtitle\"><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>");
			} else {
			print ("\n<div class=\"itemtitle\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</div>");
			}
			 print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div><br />"); 		}
	} else {
		print ("<b>There are no articles in this feed.</b>");
	}
}
print ("<span style=\"font-size:xx-small;\"><a href=\"http://www.globalsyndication.com/rss-parser\" style=\"color:white;\">Free PHP RSS Parser</a> - <a href=\"http://www.globalsyndication.com/rss-hosting\" style=\"color:white\">RSS Newsfeed Hosting</a></span>");
?>
 
0
•••
Lines 85-90:
PHP:
while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d"))
                    xml_error_string(xml_get_error_code($xml_parser)),  
                    xml_get_current_line_number($xml_parser),
xml_parser_free($xml_parser);

If I understand the code correctly, change it to this

PHP:
while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d")
                    xml_error_string(xml_get_error_code($xml_parser)),  
                    xml_get_current_line_number($xml_parser),
xml_parser_free($xml_parser));
 
0
•••
I spent a few minutes playing with it on my local host and fixed all the problems, it was mainly missing brackets and ; on the ends of rows, here is the working code :

PHP:
  <?php

/*
Created by Global Syndication's RSS Parser
http://www.globalsyndication.com/rss-parser
Modified By RMWebs
*/

set_time_limit(0);

$file = "http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/golf/rss.xml#";

$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;

function startElement($parser, $name, $attrs) {
       global $rss_channel, $currently_writing, $main;
       switch($name) {
           case "RSS":
           case "RDF:RDF":
           case "ITEMS":
               $currently_writing = "";
               break;
           case "CHANNEL":
               $main = "CHANNEL";
               break;
           case "IMAGE":
               $main = "IMAGE";
               $rss_channel["IMAGE"] = array();
               break;
           case "ITEM":
               $main = "ITEMS";
               break;
           default:
               $currently_writing = $name;
               break;
       }
}

function endElement($parser, $name) {
       global $rss_channel, $currently_writing, $item_counter;
       $currently_writing = "";
       if ($name == "ITEM") {
           $item_counter++;
       }
}

function characterData($parser, $data) {
    global $rss_channel, $currently_writing, $main, $item_counter;
    if ($currently_writing != "") {
        switch($main) {
            case "CHANNEL":
                if (isset($rss_channel[$currently_writing])) {
                    $rss_channel[$currently_writing] .= $data;
                } else {
                    $rss_channel[$currently_writing] = $data;
                }
                break;
            case "IMAGE":
                if (isset($rss_channel[$main][$currently_writing])) {
                    $rss_channel[$main][$currently_writing] .= $data;
                } else {
                    $rss_channel[$main][$currently_writing] = $data;
                }
                break;
            case "ITEMS":
                if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
                    $rss_channel[$main][$item_counter][$currently_writing] .= $data;
                } else {
                    $rss_channel[$main][$item_counter][$currently_writing] = $data;
                }
                break;
        }
    }
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d"));
                    xml_error_string(xml_get_error_code($xml_parser));  
                    xml_get_current_line_number($xml_parser);
xml_parser_free($xml_parser);
	}
}
// output HTML
 print ("<div class=\"channelname\">" . $rss_channel["TITLE"] . "</div>"); 
 print ("<div class=\"channeldescription\">" . $rss_channel["DESCRIPTION"] . "</div><br />"); 
if (isset($rss_channel["ITEMS"])) {
    if (count($rss_channel["ITEMS"]) > 0) {
        for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
            if (isset($rss_channel["ITEMS"][$i]["LINK"])) {
            print ("\n<div class=\"itemtitle\"><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>");
            } else {
            print ("\n<div class=\"itemtitle\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</div>");
            }
             print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div><br />");         }
    } else {
        print ("<b>There are no articles in this feed.</b>");
    }
}
print ("<span style=\"font-size:xx-small;\"><a href=\"http://www.globalsyndication.com/rss-parser\" style=\"color:white;\">Free PHP RSS Parser</a> - <a href=\"http://www.globalsyndication.com/rss-hosting\" style=\"color:white\">RSS Newsfeed Hosting</a></span>");
?>

Rick :)
 
0
•••
Thanks Rick,

I copied and pasted your code into my test.php document, and now it comes up with the error:

"Warning: fopen() [function.fopen]: URL file-access is disabled in the server configuration in /home/.bess/adnan278/www.golfchum.com/test.php on line 82

Warning: fopen(http://news.google.ca/news?q=golf&ie=UTF-8&output=rss) [function.fopen]: failed to open stream: no suitable wrapper could be found in /home/.bess/adnan278/www.golfchum.com/test.php on line 82
could not open XML input"

Is there something wrong with my host. Does it not support this version of PHP? More help would be appreciated Rick! :) Iv repped you anyway!
 
0
•••
adnan278 said:
Thanks Rick,

I copied and pasted your code into my test.php document, and now it comes up with the error:

"Warning: fopen() [function.fopen]: URL file-access is disabled in the server configuration in /home/.bess/adnan278/www.golfchum.com/test.php on line 82

Warning: fopen(http://news.google.ca/news?q=golf&ie=UTF-8&output=rss) [function.fopen]: failed to open stream: no suitable wrapper could be found in /home/.bess/adnan278/www.golfchum.com/test.php on line 82
could not open XML input"

Is there something wrong with my host. Does it not support this version of PHP? More help would be appreciated Rick! :) Iv repped you anyway!
"Warning: fopen() [function.fopen]: URL file-access is disabled in the server configuration in /home/.bess/adnan278/www.golfchum.com/test.php on line 82
allow_url_fopen is disabled by your host.
 
0
•••
Thanks Guys,

Just a quick update. I searched on Google for another way to embed RSS and I found a good Javascript version at http://feed2js.org/, which you can also customize through CSS. Its really good, so it seems like I wasted my time with the PHP! Thanks anyway for your efforts. Reps added to all who helped!
 
0
•••
I think its just the way the php one was coded, it was using non-standard ways of coding which made it very messy to patch up :)

Glad you got something working though :tu:
 
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back