PHP Code:
// #################### RANDOM TUTORIAL XML SOCKET ###################
class xItem {
var $xTitle;
var $xLink;
var $xDescription;
var $xImg;
}
// general vars
$sTitle = "";
$sLink = "";
$sDescription = "";
$xImg = "";
$arItems = array();
$itemCount = 0;
// ********* Start User-Defined Vars ************
// rss url goes here
$uFile = 'rss.xml';
// descriptions (true or false) goes here
$bDesc = true;
// ********* End User-Defined Vars **************
function startElement($parser, $name, $attrs) {
global $curTag;
$curTag .= "^$name";
}
function endElement($parser, $name) {
global $curTag;
$caret_pos = strrpos($curTag,'^');
$curTag = substr($curTag,0,$caret_pos);
}
function characterData($parser, $data) { global $curTag; // get the Channel information first
global $sTitle, $sLink, $sDescription;
$titleKey = "^RSS^CHANNEL^TITLE";
$linkKey = "^RSS^CHANNEL^LINK";
$descKey = "^RSS^CHANNEL^DESCRIPTION";
if ($curTag == $titleKey) {
$sTitle = $data;
}
elseif ($curTag == $linkKey) {
$sLink = $data;
}
elseif ($curTag == $descKey) {
$sDescription = $data;
}
// now get the items
global $arItems, $itemCount;
$itemTitleKey = "^RSS^CHANNEL^ITEM^TITLE";
$itemLinkKey = "^RSS^CHANNEL^ITEM^LINK";
$itemDescKey = "^RSS^CHANNEL^ITEM^DESCRIPTION";
$itemImgKey = "^RSS^CHANNEL^ITEM^IMAGE";
if ($curTag == $itemTitleKey) {
// make new xItem
$arItems[$itemCount] = new xItem();
// set new item object's properties
$arItems[$itemCount]->xTitle = $data;
}
elseif ($curTag == $itemLinkKey) {
$arItems[$itemCount]->xLink = $data;
}
elseif ($curTag == $itemDescKey) {
$arItems[$itemCount]->xDescription = $data;
}
elseif ($curTag == $itemImgKey) {
$arItems[$itemCount]->xImg = $data;
// increment item counter
$itemCount++;
}
}
// main loop
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($uFile,"r"))) {
die ("could not open RSS for 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);
// write out the items
srand((float) microtime() * 10000000);
$rand_keys = array_rand($arItems, 5);
for ($i=0;$i<count($rand_keys);$i++) {
$txItem = $arItems["$rand_keys[$i]"];
$randtut['tutlink'] = $txItem->xLink;
$randtut['xTitle'] = $txItem->xTitle;
$randtut['image'] = $txItem->xImg;
$randtut['Description'] = $txItem->xDescription;
eval("\$home[randtut] .= \"".fetch_template('vbindex_randtut')."\";");
}
theres the code
