Skip to content

Instantly share code, notes, and snippets.

@rukshn
Created August 18, 2012 04:37
Show Gist options
  • Save rukshn/3384465 to your computer and use it in GitHub Desktop.
Save rukshn/3384465 to your computer and use it in GitHub Desktop.
Using this code one can easily upload pictures to TwitPic using TwitPic API and Abraham Willium's Twitter OAuth Library and OAuth Echo
<?php
//using this code one can easily upload pictures to TwitPic using TwitPic API and Abraham Willium's Twitter OAuth class
/**
* @file
* User has successfully authenticated with Twitter. Access tokens saved to session and DB.
*/
/* Load required lib files. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
/* If access tokens are not available redirect to connect page. */
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
header('Location: ./clearsessions.php');
}
/* Get user access tokens out of the session. */
$access_token = $_SESSION['access_token'];
/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
//Twitpic magic begins here
$timestamp = time();
$nonce = md5(uniqid(rand(), true));
$consumer_key = CONSUMER_KEY;
$access_token = $_SESSION['access_token']['oauth_token'];
$access_secret = $_SESSION['access_token']['oauth_token_secret'];
$consumer_secret = CONSUMER_SECRET;
$twitpic_url = 'http://api.twitpic.com/2/upload.json';
$args['key'] = 'xxxxxx'; //This is your twitpic api key
$args['message'] = "testing twitpic from api"; //Tweet you want to make wit the picture
$args['media'] = "@imge.png" ; //Image you post. Don't forget the @
//Need to use this URL for the header for sending to TwitPic. Don't change.
$urlz = 'https://api.twitter.com/1/account/verify_credentials.json';
//Parameters for generating signature. Remember the signature is sent to twitpic, not to Twitter
$oconsumer_key = "oauth_consumer_key=" . $consumer_key;
$ononce = "oauth_nonce=" . $nonce;
$osigmethod = "oauth_signature_method=HMAC-SHA1";
$otimestamp = "oauth_timestamp=" . $timestamp;
$otoken = "oauth_token=" . $access_token;
$oversion = "oauth_version=1.0";
//creating a single base string for creating the signature. this can be done with arrays but a small error will give you a 401 header rejected by Twitter error, this can lessen the error. don't change anything.
//They are arranged in the alphabetical order as they should be, using arrays and kstort might work, but there should not be any spaces between the parameters and their values ex : oauth_token= 1234 not allowed
$singlestring = $oconsumer_key . "&" . $ononce . "&" . $osigmethod . "&" . $otimestamp . "&" . $otoken . "&" . $oversion;
//encoding the urls
$encsbs = rawurlencode($singlestring);
$encurl = rawurlencode($urlz);
$httpmode = "GET";
//single base string for the signature text is ready
$content = $httpmode . "&" . $encurl . "&" . $encsbs;
//just added this so it will be printed so we can see the singe base string is in order
echo $content;
$key = $consumer_secret.'&'.$access_secret;
//creating the signature
$signature = urlencode(base64_encode(hash_hmac('sha1', $content, $key, true)));
//putting the signature and everything we need to send to the header
$headers =
<<<EOF
OAuth realm="http://api.twitter.com/", oauth_consumer_key="$consumer_key", oauth_signature_method="HMAC-SHA1", oauth_token="$access_token", oauth_timestamp="$timestamp", oauth_nonce="$nonce", oauth_version="1.0", oauth_signature="$signature"
EOF;
//using curl the request is sent to twitpic not twitter
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $twitpic_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'X-Verify-Credentials-Authorization: ' . $headers,
'X-Auth-Service-Provider: ' . $urlz
));
//We post your $args array with you api key, message, and media...
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $args);
$response = curl_exec($curl);
if (!$response) {
$response = curl_error($curl);
}
curl_close($curl);
//Twitpic json respone
var_dump($response);
//if the twitpic sends JSON data as the twitpic API say then it's all good. 401 header rejected error means there is something wrong with the signature. but with this code you can't go wrong because all you need to change is the API key for twitpic the image and the message
?>
<?php
//This is some what improved version of the tiwipic.php file. You can assign this with a html form with a file upload field and a text area. You only have to change the same parameters and names of html form according to the php file
/* Load required lib files. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
/* If access tokens are not available redirect to connect page. */
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
header('Location: ./clearsessions.php');
}
/* Get user access tokens out of the session. */
$access_token = $_SESSION['access_token'];
/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
$frnd = rand (1,1000);
$pic = $_FILES["file"]["tmp_name"];
}
$timestamp = time();
$nonce = md5(uniqid(rand(), true));
$consumer_key = CONSUMER_KEY;
$access_token = $_SESSION['access_token']['oauth_token'];
$access_secret = $_SESSION['access_token']['oauth_token_secret'];
$consumer_secret = CONSUMER_SECRET;
$imessage = $_POST["tweet"];
$twitpic_url = 'http://api.twitpic.com/2/upload.json';
$args['key'] = 'XXXXXXXXXXX'; //This is your twitpic api key
$args['message'] = $imessage;
$args['media'] = "@{$pic}" ; //Don't forget the @
$urlz = 'https://api.twitter.com/1/account/verify_credentials.json';
$oconsumer_key = "oauth_consumer_key=" . $consumer_key;
$ononce = "oauth_nonce=" . $nonce;
$osigmethod = "oauth_signature_method=HMAC-SHA1";
$otimestamp = "oauth_timestamp=" . $timestamp;
$otoken = "oauth_token=" . $access_token;
$oversion = "oauth_version=1.0";
$singlestring = $oconsumer_key . "&" . $ononce . "&" . $osigmethod . "&" . $otimestamp . "&" . $otoken . "&" . $oversion;
$encsbs = rawurlencode($singlestring);
$encurl = rawurlencode($urlz);
$httpmode = "GET";
$content = $httpmode . "&" . $encurl . "&" . $encsbs;
$key = $consumer_secret.'&'.$access_secret;
$signature = urlencode(base64_encode(hash_hmac('sha1', $content, $key, true)));
$headers =
<<<EOF
OAuth realm="http://api.twitter.com/", oauth_consumer_key="$consumer_key", oauth_signature_method="HMAC-SHA1", oauth_token="$access_token", oauth_timestamp="$timestamp", oauth_nonce="$nonce", oauth_version="1.0", oauth_signature="$signature"
EOF;
$curl = curl_init();
//We are going to call twitpic api, not twitter :
curl_setopt($curl, CURLOPT_URL, $twitpic_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'X-Verify-Credentials-Authorization: ' . $headers,
'X-Auth-Service-Provider: ' . $urlz
));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $args);
$response = curl_exec($curl);
if (!$response) {
$response = curl_error($curl);
}
curl_close($curl);
//Twitpic json response
$json_out = json_decode($response,true);
$msgt = $json_out["url"];
$msgpic = $json_out["text"];
$combinemsg = $imessage . " " . $msgt;
echo $combinemsg;
$connection->post('statuses/update', array('status' => "$combinemsg"));
?>
@rukshn
Copy link
Author

rukshn commented Aug 18, 2012

You need to replace the code below //Twitpic json response with the following code to post the pictue to Twitter

$json_out = json_decode($response,true);
$msgt = $json_out["url"];
$msgpic = $json_out["text"];
$combinemsg = $imessage . " " . $msgt;

$connection->post('statuses/update', array('status' => "$combinemsg"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment