Create Twitter RSS Feeds with php and Simple html dom

Twitter_logo-3Looking for a nice way to make a RSS feed from your Twitter account? Here is one solution for you. Twitter offers the ability to create widgets, this enables the ability to create an RSS feed using Simple HTML DOM and the PHP scrape function file_get_contents .

  1. Download and install the Simple html dom library in an optional folder  
  2. From your Twitter page, go to Settings/Widgets create a new widget. (You can create widgets for user favorites, timelines, Twitter list and search results)
  3. Once the new widget is published, copy the widget ID find in the URL of the current widget. ex. 42428569387298921
  4. Use this Widget id to access the page like this  ”twitter-rss.php?id=42428569387298921”

twitter-rss.php:

<?php
if (!isset($_GET['id'])) {
exit('Not a valid RSS feed.');
}

header('Content-Type: text/xml; charset=utf-8');

require '../simple_html_dom.php';

//Split with position
function split_nth($str, $delim, $n) {
return array_map(function($p) use ($delim) {
return implode($delim, $p);
}, array_chunk(explode($delim, $str), $n));
}

$html = file_get_contents('https://cdn.syndication.twimg.com/widgets/timelines/'.$_GET['id']);
$data = json_decode($html);

$str_data = preg_replace('/\s\s+/', ' ', $data->body);

//Create object and load data
$html_dom = new simple_html_dom();
$html_dom->load($str_data);

//RSS Title
$title = $html_dom->find('a.customisable-highlight',0);

$rss_feed = array();
$rss_feed[] = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel>';
$rss_feed[] = '<title>'.htmlspecialchars($title->title, ENT_QUOTES,'UTF-8').'</title>
<atom:link href="http://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"].'" rel="self" type="application/rss+xml" />
<link>'.$title->href.'</link>
<description>'.$title->title.'</description>';

//Scrape All Tweets
$items = $html_dom->find('li.h-entry');

foreach($items as $element) {

$date_time = $element->find('a.u-url',0);
$img_obj = $element->find('img.autosized-media',0);
$img_link = $element->find('a.photo-link',0);
$content = $element->find('p.e-entry-title',0);

//Remove random crap
$tcontent = preg_replace('/data-pre-embedded="(.*?)"|dir="ltr"|data-scribe="(.*?)"/','',$content->innertext);

//Add image if attached to the current tweet
if(is_object($img_obj)) {
$img = $img_obj->getAttribute('data-srcset');
$img_src = split_nth(urldecode($img),':',2);
$tcontent = '<a href="'.$img_link->href.'" target="_blank"><img src="'.$img_src[0].'" alt="" class="feed-img"/></a>'. $tcontent;
}

$rss_feed[] = '<item><title>';
$rss_feed[] = $date_time->childNodes(0)->innertext;
$rss_feed[] = '</title><link>'.$date_time->href.'</link>';
$rss_feed[] = '<pubDate>'.date("r", strtotime($date_time->getAttribute('data-datetime'))).'</pubDate>
<description><![CDATA['.$tcontent.']]></description>
<guid>'.$date_time->href.'</guid>
</item>';
}

$html_dom->clear();

$rss_feed[] = '</channel></rss>';

echo implode($rss_feed,'');

?>

Descrambler
Neither I nor Järna Kommunikation AB assumes no responsibility of any kind, regarding the use of this script. This script is supplied ”as is”, whether you can use it or not, find out more at the Twitter Terms Of Service .

Jesper Thörn Webbutvecklare, Nätv.tekniker 073 656 51 72 jesper@jarnakommunikation.se