Tragen sie hier ihren Suchbegriff ein.

Fotosidebar die Zweite

hey hey,

ich habe jetzt meine Foto-Leiste rechts im Layout verändert, dabei greife ich auf ein php Skript zurück, dass ich gefunden habe zum auslesen von Atom-Feeds. Vorher hatte ich ein extern gelagertes Skript benutzt. Die Vorteile des neuen sind, dass es mit php4 läuft und kein 5er braucht und ich es desshalb vollkommen modifizieren kann und mehr Freiheit habe im Look. Außerdem aktualisiert es auch schneller. Das Einbinden geht recht schnell:

include "class.myatomparser.php";
$url = "http://johnobo-fotoblog.blogspot.com/feeds/posts/default";
$atom_parser = new myAtomParser($url);
$output = $atom_parser->getOutput(3);
echo $output;
?>

Achso, ich habe mir auch Gedanken gemacht ob der Blog nicht zu breit wird für eine Auflsöung bie 1024px x 786px, aber mein Google Analytics sagt mir das mehr als 80% meiner Besucher eine höhere Auflösung benutzen. Also bleib ich erstmal dabei, aber ich überlege schon wie ich es optimieren kann. *smile*

Im Wordpress Ordner des Tehmes habe ich dann noch die libary gespeichert:

# Original PHP code by Chirp Internet: www.chirp.com.au
# Please acknowledge use of this code by including this header.

# class.myatomparser.php

class myAtomParser
{
# keeps track of current and preceding elements
var $tags = array();

# array containing all feed data
var $output = array();

# return value for display functions
var $retval = "";

var $encoding = array();

# constructor for new object
function myAtomParser($file)
{
# instantiate xml-parser and assign event handlers
$xml_parser = xml_parser_create("");
xml_set_object($xml_parser, $this);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "parseData");

# open file for reading and send data to xml-parser
$fp = @fopen($file, "r") or die("<strong>myAtomParser Error:</strong> Could not open URL $file for input");
while($data = fread($fp, 4096)) {
xml_parse($xml_parser, $data, feof($fp)) or die(
sprintf("myAtomParser: Error <strong>%s</strong> at line <strong>%d</strong>
"
,
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser))
);
}
fclose($fp);

# dismiss xml parser
xml_parser_free($xml_parser);
}

function startElement($parser, $tagname, $attrs)
{
if($this-&gt;encoding) {
# content is encoded - so keep elements intact
$tmpdata = "&lt;$tagname";
if($attrs) foreach($attrs as $key =&gt; $val) $tmpdata .= " $key=\"$val\"";
$tmpdata .= "&gt;";
$this-&gt;parseData($parser, $tmpdata);
} else {
if($attrs[‘HREF’] &amp;&amp; $attrs[‘REL’] &amp;&amp; $attrs[‘REL’] == ‘alternate’) {
$this-&gt;startElement($parser, ‘LINK’, array());
$this-&gt;parseData($parser, $attrs[‘HREF’]);
$this-&gt;endElement($parser, ‘LINK’);
}
if($attrs[‘TYPE’]) $this-&gt;encoding[$tagname] = $attrs[‘TYPE’];

# check if this element can contain others - list may be edited
if(preg_match("/^(FEED|ENTRY)$/", $tagname)) {
if($this-&gt;tags) {
$depth = count($this-&gt;tags);
list($parent, $num) = each($tmp = end($this-&gt;tags));
if($parent) $this-&gt;tags[$depth-1][$parent][$tagname]++;
}
array_push($this-&gt;tags, array($tagname =&gt; array()));
} else {
# add tag to tags array
array_push($this-&gt;tags, $tagname);
}
}
}

function endElement($parser, $tagname)
{
# remove tag from tags array
if($this-&gt;encoding) {
if(isset($this-&gt;encoding[$tagname])) {
unset($this-&gt;encoding[$tagname]);
array_pop($this-&gt;tags);
} else {
if(!preg_match("/(BR|IMG)/", $tagname)) $this-&gt;parseData($parser, "");
}
} else {
array_pop($this-&gt;tags);
}
}

function parseData($parser, $data)
{
# return if data contains no text
if(!trim($data)) return;
$evalcode = "\$this-&gt;output";
foreach($this-&gt;tags as $tag) {
if(is_array($tag)) {
list($tagname, $indexes) = each($tag);
$evalcode .= "[\"$tagname\"]";
if(${$tagname}) $evalcode .= "[" . (${$tagname} - 1) . "]";
if($indexes) extract($indexes);
} else {
if(preg_match("/^([A-Z]+):([A-Z]+)$/", $tag, $matches)) {
$evalcode .= "[\"$matches[1]\"][\"$matches[2]\"]";
} else {
$evalcode .= "[\"$tag\"]";
}
}
}

if(isset($this-&gt;encoding[‘CONTENT’]) &amp;&amp; $this-&gt;encoding[‘CONTENT’] == "text/plain") {
$data = "
<pre>$data</pre>
"
;
}

eval("$evalcode .= ‘" . addslashes($data) . "’;");
}

# display a single feed as HTML
function display_feed($data, $limit)
{
extract($data);
# if($TITLE) {
#   # display feed information
#   $this-&gt;retval .= "
<h1>";
#   if($LINK) $this-&gt;retval .= "
<a href="\" target="\&quot;_blank\&quot;">";
#   $this-&gt;retval .= stripslashes($TITLE);
#   if($LINK) $this-&gt;retval .= "</a>";
#   $this-&gt;retval .= "</h1>
\n";
#   if($TAGLINE) $this-&gt;retval .= "

" . stripslashes($TAGLINE) . "

\n\n";
#   $this-&gt;retval .= "

<div class="\&quot;divider\&quot;"><!– –></div>
\n\n";
# }
if($ENTRY) {
# display feed entry(s)
foreach($ENTRY as $item) {
$this-&gt;display_entry($item, "
FEED");
if(is_int($limit) &amp;&amp; –$limit &lt;= 0) break;
}
}
}

# display a single entry as HTML
function display_entry($data, $parent)
{
extract($data);
if(!$TITLE) return;

$this-&gt;retval .=  "
<h3>";
if($LINK) $this-&gt;retval .=  "
<a href="\" target="\&quot;_blank\&quot;">";
$this-&gt;retval .= stripslashes($TITLE);
if($LINK) $this-&gt;retval .= "</a>";
$this-&gt;retval .=  "";
if($ISSUED) $this-&gt;retval .= " <small>($ISSUED)</small>";
$this-&gt;retval .=  "</h3>
\n"
;

# if($AUTHOR) {
#   $this-&gt;retval .=  "

<strong>Author:</strong> " . stripslashes($AUTHOR['NAME']) . "

\n\n";
# }
if($CONTENT) {
$this-&gt;retval .=  "

" . stripslashes($CONTENT) . "

\n\n";
} elseif($SUMMARY) {
$this-&gt;retval .=  "

" . stripslashes($SUMMARY) . "

\n\n";
}
}

function fixEncoding($input, $output_encoding)
{
$encoding = mb_detect_encoding($input);
switch($encoding) {
case ‘ASCII’:
case $output_encoding:
return $input;
case ”:
return mb_convert_encoding($input, $output_encoding);
default:
return mb_convert_encoding($input, $output_encoding, $encoding);
}
}

# display entire feed as HTML
function getOutput($limit=false, $output_encoding=’UTF-8′)
{
$this-&gt;retval = "";
$start_tag = key($this-&gt;output);

switch($start_tag) {
case "FEED":
foreach($this-&gt;output as $feed) $this-&gt;display_feed($feed, $limit);
break;
default:
die("
Error: unrecognized start tag ‘$start_tag’ in getOutput()");
}

return $this-&gt;fixEncoding($this-&gt;retval, $output_encoding);
}

# return raw data as array
function getRawOutput($output_encoding=’UTF-8′)
{
return $this-&gt;fixEncoding($this-&gt;output, $output_encoding);
}
}
?&gt;

den Code habe ich übrigens auf the-art-of-web.com gefunden. Großartige Sache, leicht zu verstehen und funktioniert super.

Die PHP File ist leicht verständlich und desshalb ging es schnell unwichtige Sachen auszukommentieren, und die generierten HTML Tags zu bearbeiten. Kann ich empfehlen.

Aber wenn ich schon bei empfehlen in, ich habe zum Syntax highlighten ein tolles Wordpress-PlugIn gefunden Code Snippet heißt das gute Stück und unterstützt zig verschiedene Sprachen und es ist einfach zu benutzen.
aus dem readme:

Kategorien:AJAX, Allgemein, Design, Non-Print, PHP, Programmieren, Tutorial, web



ein Kommentar


kommentieren