Skip to content

Instantly share code, notes, and snippets.

@sergioccrr
Created February 10, 2013 04:16
Show Gist options
  • Save sergioccrr/4748324 to your computer and use it in GitHub Desktop.
Save sergioccrr/4748324 to your computer and use it in GitHub Desktop.
Tweetbot2Twitpic
<?php
define('API_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
function _header($code) {
$code = (int) $code;
$codes = array(
403=>'Forbidden',
404=>'Not Found',
500=>'Internal Server Error'
);
if (!array_key_exists($code, $codes)) {
return false;
}
header(sprintf('%s %s %s', $_SERVER['SERVER_PROTOCOL'], $code, $codes[$code]), true, $code);
}
$oauth = (isset($_SERVER['HTTP_X_VERIFY_CREDENTIALS_AUTHORIZATION'])) ? $_SERVER['HTTP_X_VERIFY_CREDENTIALS_AUTHORIZATION'] : '';
if ($_SERVER['REQUEST_METHOD'] != 'POST' || empty($_POST) || empty($oauth)) {
_header(403);
echo 'Nope.';
return;
}
$oauth = str_replace('OAuth', 'OAuth realm="http://api.twitter.com/",', $oauth);
$headers = array(
'X-Auth-Service-Provider: https://api.twitter.com/1/account/verify_credentials.json',
'X-Verify-Credentials-Authorization: ' . $oauth
);
$post = array(
'key'=>API_KEY,
'message'=>$_POST['message'],
'media'=>'@' . $_FILES['media']['tmp_name']
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.twitpic.com/2/upload.json');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status != 200) {
_header(500);
return;
}
$json = json_decode($result, true);
if (!isset($json['url']) || empty($json['url'])) {
_header(500);
return;
}
printf('<mediaurl>%s</mediaurl>', $json['url']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment