Skip to content

Instantly share code, notes, and snippets.

@gavinbenda
Last active December 18, 2015 10:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gavinbenda/5771059 to your computer and use it in GitHub Desktop.
Save gavinbenda/5771059 to your computer and use it in GitHub Desktop.
Adaptor for apps requiring Twitter feeds in RSS format. You'll also need to download the TwitterAPIExchange.php class from here: https://github.com/J7mbo/twitter-api-php/blob/master/TwitterAPIExchange.php Follow these instructions to setup your Twitter app: http://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline…
<?php
/*
/* enter your Twitter username here */
DEFINE('TWITTER_USERNAME', 'replace-me');
/*
/* this is where you put in all of your secret spices */
/* check out this guide for an explaination: http://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1/15314662#15314662 */
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
/*
/* Include Twitter-API-PHP: http://github.com/j7mbo/twitter-api-php */
include('TwitterAPIExchange.php');
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name='. TWITTER_USERNAME;
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$result = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
/*
/* get a nice object we can work with */
$jsonDecoded = json_decode($result);
/*
/* generate the XML */
header("Content-Type: application/rss+xml");
$dom = new DomDocument("1.0", "ISO-8859-1");
$dom->version = "1.0";
$dom->encoding = "ISO-8859-1";
$rssElem = $dom->createElement('rss');
$channelElem = $rssElem->appendChild( $dom->createElement('channel') );
foreach($jsonDecoded as $item)
{
$itemElem = $channelElem->appendChild ( $dom->createElement('item') );
$itemElem->appendChild ( $dom->createElement('title', $item->text) );
$itemElem->appendChild ( $dom->createElement('link', 'https://twitter.com/'. TWITTER_USERNAME .'/status/'. $item->id_str ) );
//$itemElem->appendChild ( $dom->createElement('description', $item->text) ); // not usually needed for Twitter RSS
}
echo $dom->saveXML($rssElem);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment