Gå til innhold

RSS feed på hjemmesiden


Anbefalte innlegg

Hei.

 

Jeg driver på å lage en hjemmeside. På denne siden så har jeg lyst til å ha en side som her en oversikt over nyheter på en rekke andre sider.

 

Jeg har fikk da høre om rss. Det høres jo ut som en bra plan og bruke dette på siden. Det jeg lurte på er om det er noen som har en kode snutt som henter ut nyheter fra feks http://www.itavisen.no/rss.php.

 

Jeg har støtte for PHP på serveren min og vil at det skal være i php kode siden det er det jeg har mest kjenskap til.

 

Håper på Hjelp.

 

mvh

Håvard

Lenke til kommentar
Videoannonse
Annonse

Torbjørn,grunnen for at dette forumet er opprettet er fordi folk skal slippe å lete seg ihel, igjennom ting som de ikke helt forstår... derfor mener jeg at du kan spare deg for dårlige google henvisninger og heller prøve å hjelpe på en bedre måte.

Beklager om jeg virker litt kvass mot deg... men jeg har sett en del av dette fenomenet på forumet i det siste.

Lenke til kommentar

En ting med Google er at den er meget efektiv :D men det er tiltider MYE engelsk der som kansje ikke alle er like flinke til å forstå. og lese til seg noe da blir vansklig.

 

Ang topic.

De fleste sidene på nett basert på php har jo en link som kalles "backend.php" denne kan brukes til å hente nyheter fra div netsteder og du kan legge den inn i en Blokk.

 

Stort sett de fleste sidene har lagt opp 10 siste i backend.php

Lenke til kommentar

Kuroshin include:

 

<?php

/*

This example demonstrates the load-method
It gets an rdf feed from slashdot.org

*/

include_once('/home/sites/site36/web/EasyBookMarker/xml/xml.php');

// Create xml object and get feed
$rss = new XML();
$rss->load('http://www.kuro5hin.org/backend.rdf');

// Display news items
echo '<h3>K5</h3>';
$items = $rss->firstChild->childNodes;
$nitems = count($items);
for ($i = 0; $i < $nitems; $i++) {
 if ($items[$i]->nodeName == 'item') {
 	$title = $items[$i]->firstChild->firstChild->nodeValue;
 	$link = $items[$i]->firstChild->nextSibling->firstChild->nodeValue;
 	echo ' •  <a href="'.$link.'" target="_blank">'.$title.'</a><br>';
 }
}

?>

 

XMLbibliotek:

 

<?php

 /*

XML class v0.8 - tries to implement the DOM XML without use of external libs
(c) copyright 2002 Bas van Gaalen

The properties and methods in this class are based on the properties and
methods of the Flash Actionscript XML object. They behave in the exact same way.

Feel free to use or redistribute this library, but please keep this copyright
message intact.

Send comments, questions or suggestions to bas-at-webtweakers-dot-com

To-do:
- implement removeNode and insertBefore methods
- optimize xml_get_children and appendChild

*/

// XMLTag sub-class: Text ==================================================
class XMLText {
 
 // Property (r); returns the node value of the XML object.
 var $nodeValue;

 function XMLText() {
 	// Static...
 }

}



// XML sub-class: Tags =====================================================
class XMLTag extends XMLText {

 // External properties --------------------------------------------------
 
 // Collection (r/w); returns an associative array containing all
 //  attributes of the specified XML object
 var $attributes;

 // Collection (r); returns an array of the specified XML object's children.
 var $childNodes;
 
 // Property (r); references the first child in the parent node's child list.
 var $firstChild;

 // Property (r); references the last child in the parent node's child list.
 var $lastChild;

 // Property (r); references the next sibling in the parent node's child list.
 var $nextSibling;

 // Property (r/w); takes or returns the node name of the XML object.
 var $nodeName;

 // Property (r) referenced the parent node of the specified XML object.
 var $parentNode;

 // Property (r); references the previous sibling in the parent node's child list.
 var $previousSibling;

 // XMLTag constructor ---------------------------------------------------
 function XMLTag() {

 	// Init external properties
 	$this->attributes = null;
 	$this->childNodes = null;
 	$this->firstChild = null;
 	$this->lastChild = null;
 	$this->nextSibling = null;
 	$this->nodeName = '';
 	$this->nodeValue = '';
 	$this->parentNode = null;
 	$this->previousSibling = null;
 }

 // Internal methods -----------------------------------------------------
 function xml_get_children($vals, &$i) {
 	$children = array();
 	$nChildren = count($vals);
 	while (++$i < $nChildren) {
   switch ($vals[$i]['type']) {
   	case 'cdata':
     break;

   	case 'complete':
     $tmp = new XMLTag();
     $tmp->nodeName = $vals[$i]['tag'];
     $tmp->attributes = isset($vals[$i]['attributes'])?$vals[$i]['attributes']:null;
     $tmp->createTextNode($vals[$i]['value']);
     $children[] = $tmp;
     break;

   	case 'open':
     $tmp = new XMLTag();
     $tmp->nodeName = $vals[$i]['tag'];
     $tmp->attributes = isset($vals[$i]['attributes'])?$vals[$i]['attributes']:null;
     $tmp->parentNode = $this;
     $tmp->childNodes = $tmp->xml_get_children($vals, $i);
     $children[] = $tmp;
     break;

   	case 'close':
     $nThisChildren = count($children);
     if ($nThisChildren > 1) {
     	for ($j = $nThisChildren-1; $j >= 0; $j--)
       $children[$j]->nextSibling = $children[$j + 1];
     	for ($j = 1; $j < $nThisChildren; $j++)
       $children[$j]->previousSibling = $children[$j - 1];
     }
     $this->firstChild = $children[0];
     $this->lastChild = $children[($nThisChildren-1) % $nThisChildren];
     return $children;
     break;

   }
 	}
 }

 // Methods --------------------------------------------------------------

 // Appends the specified child node to the XML object's child list.
 function appendChild(&$childNode) {
 	$childNode->parentNode = $this;
 	$this->childNodes[] = &$childNode;
 	$last = count($this->childNodes);
 	if ($last > 1) {
   for ($j = $last-1; $j >= 0; $j--)
   	$this->childNodes[$j]->nextSibling = $this->childNodes[$j + 1];
   for ($j = 1; $j < $last; $j++)
   	$this->childNodes[$j]->previousSibling = $this->childNodes[$j - 1];
 	}
 	$this->firstChild = $this->childNodes[0];
 	$this->lastChild = $this->childNodes[($last-1) % $last];

 }

 // Creates a new XML element with the name specified in the argument.
 function createElement($name) {
 	$tmp = new XMLTag();
 	$tmp->nodeName = $name;
 	return $tmp;
 }

 // Creates a new XML text node with the specified text.
 function createTextNode($text) {
 	$this->firstChild = new XMLText();
 	$this->firstChild->nodeValue = trim($text);
 	return $this->firstChild;
 }

 // Returns true if there are child nodes; otherwise, returns false.
 function hasChildNodes() {
 	return count($this->childNodes) > 0;
 }

 // Inserts a new child node into the XML object's child list, before the
 //  provided node
 function insertBefore(&$childNode, $beforeNode) {
 	// Not implemented yet...
 }

 // Removes the specified XML object from its parent.
 function removeNode() {
 	// Not implemented yet...
 }

 // Evalutes the specified XML object, constructs a textual representation
 //  of the XML structure including the node, children and attributes, and
 //  returns the result as a string.
 function toString() {

 	$tagOpen = "<";
 	$tagClose = ">";
 	$tagBreak = "";

 	$sAttr = "";
 	if (!empty($this->attributes)) {
   foreach ($this->attributes as $key=>$val)
   	$sAttr .= " $key=\"$val\"";
 	}

 	$retVal = "";
 	if (gettype($this->parentNode) == 'NULL') {
   $retVal .= $this->xmlDecl;
   $retVal .= $this->docTypeDecl;
 	}

 	if (!empty($this->nodeName)) {
   if ($this->hasChildNodes()) {
   	$retVal .= $tagOpen.$this->nodeName.$sAttr.$tagClose.$tagBreak;
   } elseif (empty($this->firstChild->nodeValue)) {
   	$retVal .= $tagOpen.$this->nodeName.$sAttr." /".$tagClose.$tagBreak;
   } else {
   	$retVal .= $tagOpen.$this->nodeName.$sAttr.$tagClose.$this->firstChild->nodeValue.$tagOpen."/".$this->nodeName.$tagClose.$tagBreak;
   }
 	}

 	if ($this->hasChildNodes()) {
   foreach ($this->childNodes as $child) {
   	$retVal .= $child->toString();
   }
 	}

 	if ($this->hasChildNodes() && !empty($this->nodeName)) {
   $retVal .= $tagOpen."/".$this->nodeName.$tagClose.$tagBreak;
 	}

 	return $retVal;

 }

} // End class



// Main XML class ==========================================================
class XML extends XMLTag {

 // External properties --------------------------------------------------

 // Some vars for error tracking and messages
 var $status;
 var $error;

 // Property (r/w); information about the XML document DOCTYPE decleration.
 var $docTypeDecl;

 // Property (r/w); sets and returns information about a document's XML decleration.
 var $xmlDecl;

 // XML constructor ------------------------------------------------------
 function XML($url = '') {

 	// Init external properties
 	parent::XMLTag();
 	$this->status = 0;
 	$this->error = '';
 	$this->docTypeDecl = '';
 	$this->xmlDecl = '';

 	// Load the referenced XML document
 	$this->load($url);
 }

 // Methods --------------------------------------------------------------

 // Loads an XML document from the specified URL.
 function load($url) {
 	if ($url == '') return false;
 	
 	$rawdata = implode('', file($url));
 	$this->parseXML($rawdata);
 }

 // Parses the XML text specified in the source argument.
 function parseXML($source) {
 	
 	// Clear any content that this object might have
 	// Call: $this->removeNode()

 	// Parse the xml document to an array structure
 	$parser = xml_parser_create();
 	xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
 	xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE,   1);
 	xml_parse_into_struct($parser, $source, $vals, $index);
 	xml_parser_free($parser);

 	// Set xml decleration
 	if (preg_match("/<?xml\ (.*?)\?>/i", $source, $matches)) {
   $this->xmlDecl = "<?xml ".$matches[1]."?>";
 	}

 	// Set document type decleration
 	if (preg_match("/<!doctype\ (.*?)>/i", $source, $matches)) {
   $this->docTypeDecl = "<!DOCTYPE ".$matches[1].">";
 	}

 	// parse the structure and create this object...
 	$root = $this->createElement($vals[0]['tag']);
 	$root->attributes = isset($vals[0]['attributes'])?$vals[0]['attributes']:null;
 	$root->childNodes = $root->xml_get_children($vals, $i = 0);
 	$this->appendChild($root);
 }

 // Encodes the specified XML object into a XML document and sends
 //  it to the specified URL using the POST method.
 //  $url is of the form: (http://)www.domain.com:port/path/to/file
 function send($url) {

 	// Get xml document
 	$strXML = $this->toString();

 	// Get url parts
 	if (!preg_match("/http/", $url)) $url = "http://".$url;
 	$urlParts = parse_url($url);
 	$host = $urlParts['host']?$urlParts['host']:'localhost';
 	$port = $urlParts['port']?$urlParts['port']:80;
 	$path = $urlParts['path']?$urlParts['path']:"/";

 	// Open a connection with the required host
 	$fp = fsockopen($host, $port, $errno, $errstr);
 	if (!$fp) {
   $this->status = -11;
   $this->error = "Unable to connect to $host at port $port: ($errno) $errstr";
   return false;
 	} else {

   // Send the xml document
   fputs($fp, "POST $path HTTP/1.0\n".
   	"Host: $host\n".
   	"Content-length: ".strlen($strXML)."\n".
   	"Content-type: text/xml\n\n".
   	$strXML."\n\n".
   	"Connection: close\n\n");

 	}

 	return true;

 }

 // Encodes the specified XML object into a XML document, sends
 //  it to the specified URL using the POST method, downloads the server's
 //  response and then loads it into the target.
 //  $url is of the form: (http://)www.domain.com:port/path/to/file
 function sendAndLoad($url, &$target) {

 	// Check target type, fail on wrong type
 	if (gettype($target) != 'object') {
   $this->status = -10;
   $this->error = "Target is of type '".gettype($target)."', but should be 'object'";
   return false;
 	}

 	// Get xml document
 	$strXML = $this->toString();

 	// Get url parts
 	if (!preg_match("/http/", $url)) $url = "http://".$url;
 	$urlParts = parse_url($url);
 	$host = $urlParts['host']?$urlParts['host']:'localhost';
 	$port = $urlParts['port']?$urlParts['port']:80;
 	$path = $urlParts['path']?$urlParts['path']:"/";

 	// Open a connection with the required host
 	$fp = fsockopen($host, $port, $errno, $errstr);
 	if (!$fp) {
   $this->status = -11;
   $this->error = "Unable to connect to $host at port $port: ($errno) $errstr";
   return false;
 	} else {

   // Send the xml document
   fputs($fp, "POST $path HTTP/1.0\n".
   	"Host: $host\n".
   	"Content-length: ".strlen($strXML)."\n".
   	"Content-type: text/xml\n\n".
   	$strXML."\n\n".
   	"Connection: close\n\n");

   // Recieve response
   $buf = "";
   while (!feof($fp)) $buf .= fread($fp, 128);
   fclose($fp);

   // Filter xml out response (dump http headers)
   if (preg_match("/(<.*>)/msi", $buf, $matches)) { // Greedy match
   	$xmlResponse = $matches[1];
   	$target->parseXML($xmlResponse);
   } else {
   	$this->status = -12;
   	$this->error = "Unidentified server response: no xml was sent";
   	return false;
   }

 	}

 	return true;
 }

} // End class

?>

Lenke til kommentar

Førstnevnte brukes til å inkludere feeds (en for slashdot er inkludert), og sistnevnte parser og produsere feedsene.

 

Slashdot parser:

<?php

include_once('/home/sites/site36/web/EasyBookMarker/xml/xml.php');

// Create xml object and get feed
$rss = new XML();
@$rss->load('http://slashdot.org/slashdot.rdf');

// Display news items
echo '<h3>Slashdot</h3>';
$items = $rss->firstChild->childNodes;
$nitems = count($items);
for ($i = 0; $i < $nitems; $i++) {
 if ($items[$i]->nodeName == 'item') {
 	$title = $items[$i]->firstChild->firstChild->nodeValue;
 	$link = $items[$i]->firstChild->nextSibling->firstChild->nodeValue;
 	echo ' •  <a href="'.$link.'" target="_blank">'.$title.'</a><br>';
 }
}

?>

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...