Here's the code for twic.li
function twitter_uploadpicture_page($query) {
if (user_type() == 'oauth') {
//Has the user submitted an image and message?
if ($_POST['message']) {
$messages = stripslashes($_POST['message']);
$serviceURL = 'http://twic.li/photos/upload.json';
//Set the initial headers
$header = array(
'X-Auth-Service-Provider: https://api.twitter.com/1/account/verify_credentials.json',
'X-Verify-Credentials-Authorization: OAuth realm="http://api.twitter.com/"'
);
//Using Abraham's OAuth library
require_once('OAuth.php');
// instantiating OAuth customer
$consumer = new OAuthConsumer(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);
// instantiating signer
$sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
// user's token
list($oauth_token, $oauth_token_secret) = explode('|', $GLOBALS['user']['password']);
$token = new OAuthConsumer($oauth_token, $oauth_token_secret);
// Generate all the OAuth parameters needed
$signingURL = 'https://api.twitter.com/1/account/verify_credentials.json';
$request = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $signingURL, array());
$request->sign_request($sha1_method, $consumer, $token);
$header[1] .= ", oauth_consumer_key="" . $request->get_parameter('oauth_consumer_key') .""";
$header[1] .= ", oauth_signature_method="" . $request->get_parameter('oauth_signature_method') .""";
$header[1] .= ", oauth_token="" . $request->get_parameter('oauth_token') .""";
$header[1] .= ", oauth_timestamp="" . $request->get_parameter('oauth_timestamp') .""";
$header[1] .= ", oauth_nonce="" . $request->get_parameter('oauth_nonce') .""";
$header[1] .= ", oauth_version="" . $request->get_parameter('oauth_version') .""";
$header[1] .= ", oauth_signature="" . urlencode($request->get_parameter('oauth_signature')) .""";
//open connection
$ch = curl_init();
//Set paramaters
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$serviceURL);
$media_data = array(
'media' => '@'.$_FILES['media']['tmp_name'],
'message' => ' ' . $messages, //A space is needed because twitpic b0rks if first char is an @
'key'=>TWICLI_API_KEY
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$media_data);
//execute post
$result = curl_exec($ch);
$response_info=curl_getinfo($ch);
//close connection
curl_close($ch);
if ($response_info['http_code'] == 200) { //Success
//Decode the response
$json = json_decode($result);
$id = $json->slug;
$imgurl = $json->mediaurl;
$message = trim($messages) . " " . $imgurl;
//Send the user's message to twitter
$request = API_URL.'statuses/update.json';
$post_data = array('source' => 'Rupi', 'status' => $message);
$status = twitter_process($request, $post_data);
//Back to the timeline
twitter_refresh("upload picture/confirm/$id");
}
else {
$content = "twic.li upload failed. No idea why!";
$content .= "";
$json = json_decode($result);
$content .= "message " . urlencode($_POST['message']);
$content .= "json " . print_r($json);
$content .= "Response " . print_r($response_info);
$content .= "header " . print_r($header);
$content .= "media_data " . print_r($media_data);
$content .= "URL was " . $serviceURL;
$content .= "File uploaded was " . $_FILES['media']['tmp_name'];
$content .= "";
}
}
elseif ($query[1] == 'confirm') {
// because the thumbnail is big enough, I think better using the Sencha.io again. 🙂
$content = "Upload success. Image posted to Twitter.
";
}
else {
$content = "Image Message (optional):110";
$content .= js_counter("message", "110");
}
return theme('page', 'Twic.li Upload', $content);
}
}