Web Hosting Talk







View Full Version : php xml feed


a11c
04-05-2010, 07:14 PM
hello all,

i have a php script that pulls xml feed from various websites. The script works fine when only using once per page. I would like to modify the coding to allow me to use the same script 2 times per page. here are my files and coding

when i call on the file i use <?php include ($_SERVER["DOCUMENT_ROOT"]."/rss/rss.php"); ?>
the other is <?php include ($_SERVER["DOCUMENT_ROOT"]."/rss/sidebarvideo.php"); ?>

now i have 2 php files, the php files look like this; the only difference is on line 4. one uses /rss/rsslist.txt & the other is /rss/video.txt

<?php

$nr_news=15;
$rssfile=$_SERVER["DOCUMENT_ROOT"]."/rss/rsslist.txt";
$rssurls=file($rssfile);
$rand=rand(1,count($rssurls))-1;
$file = $rssurls[$rand];

$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 "ITEMS":
if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
$rss_channel[$main][$item_counter][$currently_writing] .= $data;
} else {
//print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
$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)));
}
}
*/
$data=curl_string($file);
xml_parse($xml_parser,$data);
xml_parser_free($xml_parser);

// putting in array
$news=array();
if (isset($rss_channel["ITEMS"]))
{
if (count($rss_channel["ITEMS"]) > 0)
for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) $news[]=$rss_channel["ITEMS"][$i];
}
$c=0;
foreach($news as $key=>$val)
{
if($c<$nr_news)
{
echo "<p align=left>";
echo '<a href="'.$val['LINK'].'">'.$val['TITLE'].'</a> - '.$val['PUBDATE'].'<br>'.''.$val['DESCRIPTION'].'</font></p>';
}
$c++;
}


function curl_string ($url,$user_agent='Mozilla 5.0'){

$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
$result = curl_exec ($ch);
curl_close($ch);
return $result;

}

?>

now when i check the browser i get a fatal error.

Fatal error: Cannot redeclare startelement() (previously declared in /home1/bigmoney/public_html/sitename/rss/video.php:14) in /home1/bigmoney/public_html/sitename/rss/sidebarvideo.php on line 36


can anyone help me configure to allow both to work together

webhoststudent
04-05-2010, 11:30 PM
Move all your functions into another php file and then include that prior to your other include statements. You could even use require_once just to be safe

StephenJacob
04-06-2010, 10:11 AM
Yep, that should fix ya up.

a11c
04-06-2010, 10:14 AM
i have very little experience with php

i should add all the functions into a separate file like

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

and then add a include into the php file like
<?php

include ($_SERVER["DOCUMENT_ROOT"]."/newfile.php");

$nr_news=15;
$rssfile=$_SERVER["DOCUMENT_ROOT"]."/rss/rsslist.txt";
$rssurls=file($rssfile);
$rand=rand(1,count($rssurls))-1;
$file = $rssurls[$rand];

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

is this how i should do that also where would i add require_once ?

thanks again for the quick response,

StephenJacob
04-06-2010, 10:58 AM
You got it! Just be sure to include the functions file above your additional includes. The previous poster was suggesting using include_once rather than just include.

include_once ($_SERVER["DOCUMENT_ROOT"]."/newfile.php");

a11c
04-06-2010, 07:31 PM
i did everything as described. im getting this error now

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 "ITEMS": if (isset($rss_channel[$main][$item_counter][$currently_writing])) { $rss_channel[$main][$item_counter][$currently_writing] .= $data; } else { //print ("rss_channel[$main][$item_counter][$currently_writing] = $data
"); $rss_channel[$main][$item_counter][$currently_writing] = $data; } break; } } } function curl_string ($url,$user_agent='Mozilla 5.0'){ $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ch, CURLOPT_TIMEOUT, 120); $result = curl_exec ($ch); curl_close($ch); return $result; }
Fatal error: Call to undefined function curl_string() in /home1/bigmoney/public_html/sitename/rss/rss.php on line 33

my php files look like feed.php

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 "ITEMS":
if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
$rss_channel[$main][$item_counter][$currently_writing] .= $data;
} else {
//print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
$rss_channel[$main][$item_counter][$currently_writing] = $data;
}
break;
}
}
}

function curl_string ($url,$user_agent='Mozilla 5.0'){

$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
$result = curl_exec ($ch);
curl_close($ch);
return $result;

}

and rss.php
<?php

include_once($_SERVER["DOCUMENT_ROOT"]."/rss/feed.php");

$nr_news=15;
$rssfile=$_SERVER["DOCUMENT_ROOT"]."/rss/rsslist.txt";
$rssurls=file($rssfile);
$rand=rand(1,count($rssurls))-1;
$file = $rssurls[$rand];

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

$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)));
}
}
*/
$data=curl_string($file);
xml_parse($xml_parser,$data);
xml_parser_free($xml_parser);

// putting in array
$news=array();
if (isset($rss_channel["ITEMS"]))
{
if (count($rss_channel["ITEMS"]) > 0)
for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) $news[]=$rss_channel["ITEMS"][$i];
}
$c=0;
foreach($news as $key=>$val)
{
if($c<$nr_news)
{
echo "<p align=left>";
echo '<a href="'.$val['LINK'].'">'.$val['TITLE'].'</a> - '.$val['PUBDATE'].'<br>'.''.$val['DESCRIPTION'].'</font></p>';
}
$c++;
}

?>

now i delete line 33 $data=curl_string($file); just to see what happens.

my page looks like this
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 "ITEMS": if (isset($rss_channel[$main][$item_counter][$currently_writing])) { $rss_channel[$main][$item_counter][$currently_writing] .= $data; } else { //print ("rss_channel[$main][$item_counter][$currently_writing] = $data
"); $rss_channel[$main][$item_counter][$currently_writing] = $data; } break; } } } function curl_string ($url,$user_agent='Mozilla 5.0'){ $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ch, CURLOPT_TIMEOUT, 120); $result = curl_exec ($ch); curl_close($ch); return $result; }

any ideas

a11c
04-06-2010, 07:46 PM
this is embarrassing, my lack of experience with php made this issue.

i forgot the <?php ?> in feed.php

duh. anyways thanks for all the time and help

StephenJacob
04-07-2010, 10:05 AM
Don't be embarrassed, things like this happen to me all the time and I have years of PHP experience! anyway, good job and good luck.