Skip to content

Instantly share code, notes, and snippets.

@sbisbee
Forked from cowboy/twitpic_flickr.php
Created October 18, 2011 14:42
Show Gist options
  • Save sbisbee/1295605 to your computer and use it in GitHub Desktop.
Save sbisbee/1295605 to your computer and use it in GitHub Desktop.
Twitpic-Flickr bridge. Now I can upload photos to Flickr via my iPhone Twitter app, yay!
<?php
# Twitpic-Flickr bridge - v0.1pre - 10/18/2011
# http://benalman.com/
#
# Copyright (c) 2011 "Cowboy" Ben Alman
# Dual licensed under the MIT and GPL licenses.
# http://benalman.com/about/license/
# There's no way I'm writing all the Flickr stuff myself.
# http://phpflickr.com/
require_once('phpFlickr/phpFlickr.php');
# The Flickr URL shortening service expects a base58-encoded photo id.
# This function was borrowed from http://gul.ly/d50
function base58_encode($num) {
$alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
$base_count = strlen($alphabet);
$encoded = '';
while ($num >= $base_count) {
$div = $num / $base_count;
$mod = ($num - ($base_count * intval($div)));
$encoded = $alphabet[$mod] . $encoded;
$num = intval($div);
}
if ($num) {
$encoded = $alphabet[$num] . $encoded;
}
return $encoded;
}
# Create an app: http://www.flickr.com/services/apps/create/apply/
# Edit your app flow: http://www.flickr.com/services/apps/YOUR_APP_ID/auth/
# * Check "Web application"
# * Callback URL needs to be the URL of this PHP script.
# Fill in these values: http://www.flickr.com/services/apps/YOUR_APP_ID/key/
$apikey = 'YOUR_API_KEY';
$secret = 'YOUR_SECRET';
$f = new phpFlickr($apikey, $secret);
unset($apikey, $secret);
session_start();
if ($_GET['frob']) {
$token_obj = $f->auth_getToken($_GET['frob']);
$_SESSION['token'] = $token_obj['token'];
//TODO make sure they really sent us a token (never trust third parties)
$message = $_SESSION['msg'];
$media = $_SESSION['fileName'];
unset($_SESSION['msg'], $_SESSION['fileName']);
}
else if(empty($_SESSION['token'])) {
$_SESSION['msg'] = $_POST['message'];
$_SESSION['fileName'] = $_FILES['media']['tmp_name'];
// Don't have auth, so we're going to be redirected elsewhere
$f->auth('write');
//THIS DOES NOT RUN!!!!!
}
else {
$message = $_POST['message'];
$media = $_FILES['media']['tmp_name'];
}
# Customize these as-needed.
$source = 'Cowboy!';
$description = '(posted to twitter)';
$tags = 'flickr iphone';
# Upload photo to Flickr.
$f->setToken($_SESSION['token']);
$photoid = $f->sync_upload($media, $message, $description, $tags, 1);
if ($photoid) {
$url = 'http://flic.kr/p/' . base58_encode($photoid);
} else {
$url = '';
}
# Write out the Flickr URL.
echo "<mediaurl>$url</mediaurl>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment