Skip to content

Instantly share code, notes, and snippets.

@litzinger
Created March 8, 2012 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save litzinger/1998157 to your computer and use it in GitHub Desktop.
Save litzinger/1998157 to your computer and use it in GitHub Desktop.
Cross Domain XML to JSON
/*
* JavaScript Pretty Date
* Copyright (c) 2011 John Resig (ejohn.org)
* Licensed under the MIT and GPL licenses.
*/
// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(time){
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
diff = (((new Date()).getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
return;
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}
// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
jQuery.fn.prettyDate = function(){
return this.each(function(){
var date = prettyDate(this.title);
if ( date )
jQuery(this).text( date );
});
};
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="date.js"></script>
<script type="text/javascript">
$(function(){
var base_url = 'http://domain.com/path-to-files/';
$.getJSON(base_url+'xml-to-json/proxy.php?feed=https://feeds.foursquare.com/history/3MLPIJ0XXAUVJTJOWBGQD1TD3FTBP1M3.rss', function(data)
{
$('.tweet_wrapper').html('');
if( ! data)
{
$('.tweet_wrapper').append('<p>No data found.</p>');
return;
}
if(data.error)
{
$('.tweet_wrapper').append('<p>'+ data.error +'</p>');
return;
}
$.each(data.rss.channel.item, function(key, val)
{
var item = '<p>'+ val.description +'<a href="'+ val.link +'" class="timeago" target="_blank">'+ prettyDate(val.pubDate) +'</a></p>';
$('.tweet_wrapper').append(item);
});
});
});
</script>
</head>
<body>
<section class="recent-updates">
<h4>Recent Checkins</h4>
<div class="tweet">
<div class="tweet_wrapper">
<p>Loading...</p>
</div>
</div>
</section>
</body>
</html>
<?php
set_include_path(
PATH_SEPARATOR . 'Zend/'
. PATH_SEPARATOR . get_include_path()
);
require 'Zend/Json.php';
// $_GET['feed'] = 'https://feeds.foursquare.com/history/4QXQCZRSPGNMYF0TT0JLXXJ4VOCO5SLO.rss';
if(isset($_GET['feed']) AND $_GET['feed'] != '')
{
$ch = curl_init($_GET['feed']);
header("Access-Control-Allow-Origin: *");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/xml; charset=UTF-8",
"Access-Control-Allow-Origin: *"
));
$xml = curl_exec($ch);
// Because Firefox can't handle the date format returned from Foursquare
$xml = preg_replace_callback('/<pubDate>(.+?)<\/pubDate>/', 'fix_date', $xml);
curl_close($ch);
echo Zend_Json::fromXml($xml, true);
}
else
{
echo json_encode(array('error' => 'No feed defined.'));
}
function fix_date($matches)
{
if(isset($matches[1]))
{
return '<pubDate>'. date('Y-m-d\TH:i:s\Z', strtotime($matches[1])) .'</pubDate>';
}
else
{
return $matches[1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment