How to get the rss reader worked with curl

software development

#1

I have been trying to get the rss reader worked with curl on dreamhost, but I never got it worked.

I have read the wiki on dreamhost but I still got stuck on the while loop. I am not a programmer, can anyone give me a hand? thanks!


<?php set_time_limit(0); //$file = "http://www.gamehouse.com/rss/rss_toptenonline.jsp"; $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"); $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, 'http://www.gamehouse.com/rss/rss_toptenonline.jsp'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $fp = curl_exec($ch); curl_close($ch); /* 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); 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
" . $rss_channel["ITEMS"][$i]["TITLE"] . "
"); } else { print ("\n
" . $rss_channel["ITEMS"][$i]["TITLE"] . "
"); } print ("
" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "

"); } } else { print ("No News Found"); } } ?>

#2

I am having something of the same problems, I have tried multiple php scripts, Crispy Rss reader and last Rss reader and both fail…

I have found that my rss readers dont relay the entire error message, it says its unable to open rss feed, but the reason is the server returns this message

remote host file access not supported

now I did some searching and it is said that this may be because the php is running in a safe mode?
http://us2.php.net/manual/en/features.safe-mode.php

One of the more commonly used functions that is blocked is fopen,

http://us2.php.net/manual/en/features.safe-mode.functions.php

which is what your script does right here
if (!($fp = fopen($file, “r”))) {
die(“could not open XML input”);
}

So, the problem is fopen being prevented, Does anyone know a solution to this? What is the right way to do this in this mode?

or can we disable this? I disabled extra securities on my domain, but I still have the same problem…

Any help would be appreciated


#3

You’re on the right track, jpy08. The fopen section commented on by CMNetworx is actually commented out. I tweaked the code a little bit, and you’re getting a successful response with the RSS data. Now it’s just a matter of looping over the returned XML as a string, rather than a file using fread. This is sort of interesting to me, so I’ll keep messing with it and keep you posted. Run this for proof that you’re headed in the right direction.

<?php set_time_limit(0); //$file = "http://www.gamehouse.com/rss/rss_toptenonline.jsp"; $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"); $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, 'http://www.gamehouse.com/rss/rss_toptenonline.jsp'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); echo(curl_exec($ch)); curl_close($ch); ?>

BC Tech
Team Shocker


#4

OK, I got it to work. You just needed to take the response string and plug it in to the functionality that was already there, more or less. I have pasted the code below, but I also uploaded it to my host since formatting gets screwed up in this forum. You can just copy this text file and rename it with a php extension… unless you like poor formatting. http://benconley.net/curl/gamehouseRSS.txt

<?php set_time_limit(0); $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"); $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, 'http://www.gamehouse.com/rss/rss_toptenonline.jsp'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $responseText = curl_exec($ch); curl_close($ch); xml_parse_into_struct($xml_parser, $responseText, $vals, $index); xml_parser_free($xml_parser); 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
" . $rss_channel["ITEMS"][$i]["TITLE"] . "
"); } else { print ("\n
" . $rss_channel["ITEMS"][$i]["TITLE"] . "
"); } print ("
" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "

"); } } else { print ("No News Found"); } } ?>

BC Tech
Team Shocker