Created
October 18, 2011 14:31
-
-
Save cowboy/1295564 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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
# 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); | |
# You might need to create this file and set it to be writable. Is a file even | |
# necessary? Can twitter apps store this token in a session? | |
$tokenfile = 'twitpic_flickr_token.txt'; | |
if (file_exists($tokenfile)) { | |
$token = file_get_contents($tokenfile); | |
} | |
if ($_GET['frob']) { | |
# Token passed from Flickr, store it in the token file and exit. Does this | |
# have to exit here? Can it continue on? | |
$token_obj = $f->auth_getToken($_GET['frob']); | |
$token = $token_obj['token']; | |
file_put_contents($tokenfile, $token); | |
exit(); | |
} elseif ($token == '') { | |
# No token, request one from Flickr. | |
$f->auth('write'); | |
} | |
# Set the token to enable uploading. | |
$f->setToken($token); | |
# Customize these as-needed. | |
$source = 'Cowboy!'; | |
$description = '(posted to twitter)'; | |
$tags = 'flickr iphone'; | |
# Upload photo to Flickr. | |
$message = $_POST['message']; | |
$media = $_FILES['media']['tmp_name']; | |
$photoid = $f->sync_upload($media, $message, $description, $tags, 1); | |
# 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; | |
} | |
if ($photoid) { | |
$url = 'http://flic.kr/p/' . base58_encode($photoid); | |
} else { | |
$url = ''; | |
} | |
# Write out the Flickr URL. | |
?><mediaurl><?= $url ?></mediaurl> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment