Skip to content

Instantly share code, notes, and snippets.

@shupp
Created July 10, 2010 20:38
Show Gist options
  • Save shupp/471002 to your computer and use it in GitHub Desktop.
Save shupp/471002 to your computer and use it in GitHub Desktop.
<?php
// PHP version of SimpleGeo's foursquare integration example:
// http://blog.simplegeo.com/2010/07/09/explore-your-foursquare-checkins-on-a-map-with-simplegeo/
//
// by Bill Shupp
// http://blog.shupp.org/2010/07/10/importing-foursquare-checkins-into-simplegeo-with-php/
require_once 'Services/SimpleGeo.php';
// SimpleGeo Settings
$OAuthToken = 'YOUR_SG_OAUTH_TOKEN';
$OAuthSecret = 'YOUR_SG_OAUTH_TOKEN_SECRET';
$layer = 'com.foursquare.YOUR_4SQ_NAME_HERE.checkins';
// foursquare feed URL
$fsURL = 'YOUR_4SQ_FEED_URL';
$sg = new Services_SimpleGeo($OAuthToken, $OAuthSecret);
$http = new HTTP_Request2($fsURL);
$http->setAdapter('curl');
// Grab your feed from FourSquare
$rawFeed = $http->send()->getBody();
$xml = simplexml_load_string($rawFeed);
$records = array();
// Translate the foursquare checkins to records
foreach ($xml->channel->item as $item) {
$georss = $item->children('http://www.georss.org/georss');
$coords = explode(' ', $georss->point);
$lat = $coords[0];
$lon = $coords[1];
$record = new Services_SimpleGeo_Record(
$layer,
base64_encode((string) $item->guid), // (encoded, no slashes allowed)
$lat,
$lon,
'object',
strtotime($item->pubDate)
);
$record->name = (string) $item->title;
$record->venue_link = (string) $item->link;
$records[] = $record;
}
// Send to SimpleGeo in batches of 90 or less
$batches = array_chunk($records, 90, true);
$count = 1;
foreach ($batches as $batch) {
echo "Sending batch {$count}\n";
try {
$sg->addRecords($layer, $batch);
} catch (Services_SimpleGeo_Exception $e) {
echo "ERROR: " . $e->getMessage() . " (#" . $e->getCode() . ")\n";
}
$count++;
}
echo "Done!\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment