Quote:
|
Originally Posted by RADiSTAR Ela Seeker. I have an instant RSS solution for Yahoo! feeds if your sites are on Windows servers. It might take a while to convert it to unix/linux servers. |
The folowing code has served me extremely well. It parses through XML content, and sends you an array you can pull data out of.
Code:
function GetXMLTreeData($data)
{
$parser = xml_parser_create('ISO-8859-1');
// xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $vals, $index);
xml_parser_free($parser);
$tree = array();
$i = 0;
if (isset($vals[$i]['attributes'])) {
$tree[$vals[$i]['tag']]['ATTRIBUTES'] = $vals[$i]['attributes'];
$tree[$vals[$i]['tag']][] = GetChildren($vals, $i);
}
else
$tree[$vals[$i]['tag']][] = GetChildren($vals, $i);
return $tree;
}
function GetChildren($vals, &$i)
{
$children = array(); // Contains node data
/* Node has CDATA before it's children */
if (isset($vals[$i]['value']))
$children['VALUE'] = $vals[$i]['value'];
/* Loop through children */
while (++$i < count($vals))
{
switch ($vals[$i]['type'])
{
/* Node has CDATA after one of it's children
(Add to cdata found before if this is the case) */
case 'cdata':
if (isset($children['VALUE']))
$children['VALUE'] .= $vals[$i]['value'];
else
$children['VALUE'] = $vals[$i]['value'];
break;
/* At end of current branch */
case 'complete':
if (isset($vals[$i]['attributes'])) {
$children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
$index = count($children[$vals[$i]['tag']])-1;
if (isset($vals[$i]['value']))
$children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value'];
else
$children[$vals[$i]['tag']][$index]['VALUE'] = '';
} else {
if (isset($vals[$i]['value']))
$children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value'];
else
$children[$vals[$i]['tag']][]['VALUE'] = '';
}
break;
/* Node has more children */
case 'open':
if (isset($vals[$i]['attributes'])) {
$children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
$index = count($children[$vals[$i]['tag']])-1;
$children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index],GetChildren($vals, $i));
} else {
$children[$vals[$i]['tag']][] = GetChildren($vals, $i);
}
break;
/* End of node, return collected data */
case 'close':
return $children;
}
}
}
function array_detail($obj) {
global $__level_deep;
if (!isset($__level_deep)) $__level_deep = array();
if (is_object($obj))
print '[obj]';
elseif (is_array($obj)) {
foreach(array_keys($obj) as $keys) {
array_push($__level_deep, "[".$keys."]");
printa($obj[$keys]);
array_pop($__level_deep);
}
}
else print implode(" ",$__level_deep)." = $obj";
}
I've used this code to pull results from different RSS sources. Just use the "print_r" function in php to see what you got, and just refer to whichever node you need.
Here's some code to READ the RSS from a remote server, and save the latest feed locally as a text document (so you're not pull the RSS every time someone views your page):
Code:
$url = "http://rss.topix.net/rss/food/dieting.xml";
$url = parse_url($url);
$sock = fsockopen($url["host"],"80",$errno,$error);
if (!$sock)
{
echo ("<b>Could Not Connect To Server.</b> ERROR No.: <I>$errno</I> | ERROR: <i>$error</i>");
exit;
}$result = "";
$useragent = $_SERVER['HTTP_USER_AGENT'];
$header = "";
$header .= "GET ".$url["path"]." HTTP/1.0\r\n";
$header .= "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg\r\n";
$header .= "Accept-Language: en-us\r\n";
$header .= "Host: ".$url["host"]."\r\n";
$header .= "User-Agent: $useragent\r\n";
$header .= "\r\n\r\n";
fputs($sock,$header);
$getcontent = 0;
while(!feof($sock)){
if($getcontent<1 && $result!="\r\n")
$result = fgets($sock,128);
else
{
if($getcontent<1)$result="";
$result .= fgets($sock,128);
$getcontent++;
}
}
fclose($sock);
$filename = "dieting.xml";
// Let's make sure the file exists and is writable first.
if (!file_exists($filename)||is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $result will go when we fwrite() it.
if (!$handle = fopen($filename, 'w+')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $result to our opened file.
if (fwrite($handle, $result) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($result) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
print "<hr>";
print $result;
Finally, this code will READ the XML formatted document (using the functions I first quoted above), and display the results in HTML...
Code:
$output = file("newsfeed/dieting.xml");
$output = implode("",$output);
//In Case of Problem
if(!$output){$output = file("newsfeed/dieting_backup.xml");$output = implode("",$output);}
require_once('./newsfeed/functions_extra.php');
$output = GetXMLTreeData($output);
$feed = $output["RDF:RDF"][0]["ITEM"];
$newsfeed = "";
foreach($feed as $nf){
//TITLE,LINK,DESCRIPTION
$newsfeed .= "<a href=\"".$nf["LINK"][0]["VALUE"]."\" style=\"font-size:12px;color:#000000;\"><b>".$nf["TITLE"][0]["VALUE"]."</b></a><br>";
$newsfeed .= "<small style=\"font-size:11px;color:#000000;\">".$nf["DESCRIPTION"][0]["VALUE"]."</small><br><br>";
}
$newsfeed = "<table cellpadding=10 cellspacing=0 border=0><tr><td>".addslashes($newsfeed)."</td></tr></table>";
You'll have to crawl through the details, but once it makes sense, you'll pretty much be unstoppable with grabbing RSS feeds. You just have to know a little PHP.
For a more "packaged" solution, here's a moldy oldey that you might be able to modify to your own needs.
http://www.dansteinman.com/php/pheadlines/
DEMO:
http://www.dansteinman.com/php/phead...headlines.php3
This bad boy works like a charm, caches results, but... I'm not sure how up-to-date it is (and doesn't show examples from Yahoo's feeds).
Cheers,
~ Nexus