Skip to content

Instantly share code, notes, and snippets.

@episod
Created July 26, 2010 16:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save episod/490753 to your computer and use it in GitHub Desktop.
Save episod/490753 to your computer and use it in GitHub Desktop.
<?php
include ("oAuth.php");
$noisy_debug_mode = 1;
define("twitpicApiKey", "twitPic_Api_key");
define("consumerSecret", 'myConsumerSecret');
define("consumerKey", "myConsumerKey");
define("oauthToken", "myAccessToken");
define("oauthTokenSecret", "myAccessTokenSecret");
# This is the end point at the OAuth Echo provider
define("twitpicUploadUrl", "http://api.twitpic.com/2/upload.json");
# This is the end point at Twitter that we want to Echo
define("twitterEchoUrl", "https://api.twitter.com/1/account/verify_credentials.json");
$fileToUpload = "@".$_SERVER["DOCUMENT_ROOT"] . "/labs/twitpic/shot.png";
# Prepare an OAuth Consumer
$consumer = new OAuthConsumer(consumerKey, consumerSecret, NULL);
# Prepare an Access Token to represent the user
$access_token = new OAuthToken(oauthToken, oauthTokenSecret);
# Setup the mock request object so we can sign the request
$request = OAuthRequest::from_consumer_and_token($consumer, $access_token, 'GET', twitterEchoUrl, NULL);
# Sign the constructed OAuth request using HMAC-SHA1
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, $access_token);
# We need to chop the header a bit to get it in the exact format
# that TwitPic likes best.
$header = $request->to_header();
# Snip out the "Authorization: " part, and add a realm to be polite
$real_header = explode("Authorization: OAuth", $header);
$header = 'OAuth realm="http://api.twitter.com/",' . $real_header[1];
# Keep the spaces between elements, even though we shouldn't have to
$header = str_replace(",", ", ", $header);
if ($noisy_debug_mode == 1) {
print_r("X-Verify-Credentials-Authorization:\n" . $header . "\n");
print_r("Signature Base String:\n" . $request->get_signature_base_string() . "\n");
}
$post["key"] = twitpicApiKey;
$post["message"] = "Message";
$post["media"] = $fileToUpload;
$response = send_echo_request("POST", twitpicUploadUrl, $header, $post, twitterEchoUrl);
# Evaluate the response
print_r($response);
function send_echo_request($http_method, $url, $auth_header, $postData, $echo_url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
// Set our OAuth Echo headers
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'X-Verify-Credentials-Authorization: ' . $auth_header,
'X-Auth-Service-Provider: ' . $echo_url
));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($curl);
if (!$response) {
$response = curl_error($curl);
}
curl_close($curl);
return $response;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment