Last active
December 26, 2015 18:39
-
-
Save RudolfHattenkofer/7195657 to your computer and use it in GitHub Desktop.
RSS-Feed für TUMonline Seiten. Für ERA vorkonfiguriert.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Feed URL und Name der Überschrift hier eingeben: | |
$feed_url = 'http://www.lrr.in.tum.de/public/VorlesungERAWS13'; | |
$headline = 'Aktuelles'; | |
// Feed-interne Einstellungen | |
$config = array( | |
'name' => 'Vorlesung ERA WS 13/14', | |
'desc' => 'RSS-Feed zur Vorlesung', | |
'url' => 'http://www.lrr.in.tum.de/public/VorlesungERAWS13', | |
); | |
/* * * * * * * * * * * * * * * * * * | |
* Ab hier nicht mehr bearbeiten! * | |
* * * * * * * * * * * * * * * * * */ | |
// Get raw data from server | |
$raw_html = file_get_contents( $feed_url ); | |
// Get news part | |
$after_headline = explode( '<h3>' . $headline . '</h3>', $raw_html )[1]; | |
$news_content = explode( '</ul>', $after_headline )[0]; | |
$news_content = str_replace( '<ul>', '', $news_content ); | |
// Strip list points apart | |
$news_content = str_replace( '</li>', '', $news_content ); | |
$news_parts = explode( '<li>', $news_content ); | |
// Convert news to proper array | |
$news = array(); | |
foreach( $news_parts as $entry ) { | |
if( empty( $entry ) ) | |
continue; | |
$date = substr( $entry, 0, 10 ); | |
$content = substr( $entry, 12 ); | |
$news[] = array( | |
'date' => $date, | |
'content' => $content, | |
); | |
} | |
// Generate RSS | |
// # # # # # # | |
$rss_dom = new DOMDocument( '1.0', 'utf-8' ); | |
$rss = $rss_dom->createElement( 'rss' ); | |
$channel = $rss_dom->createElement( 'channel' ); | |
// Set version | |
$rss->setAttribute( 'version', '2.0' ); | |
// Append to DOM | |
$rss_dom->appendChild( $rss ); | |
$rss->appendChild( $channel ); | |
// Set channel stuff | |
$channel->appendChild( $rss_dom->createElement( 'title', $config['name'] ) ); | |
$channel->appendChild( $rss_dom->createElement( 'description', $config['desc'] ) ); | |
$channel->appendChild( $rss_dom->createElement( 'link', $config['url'] ) ); | |
$channel->appendChild( $rss_dom->createElement( 'ttl', 0 ) ); | |
foreach( $news as $entry ) { | |
$item = $rss_dom->createElement( 'item' ); | |
$item->appendChild( $rss_dom->createElement( 'title', $entry['date'] ) ); | |
$item->appendChild( $rss_dom->createElement( 'link', $feed_url ) ); | |
$item->appendChild( $rss_dom->createElement( 'description', $entry['content'] ) ); | |
$channel->appendChild( $item ); | |
} | |
// And output it | |
header( 'Content-Type: application/rss+xml; charset=utf-8' ); | |
echo $rss_dom->saveXML(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment