-
-
Save codingjester/1649885 to your computer and use it in GitHub Desktop.
<?php | |
#Requires PHP 5.3.0 | |
define("CONSUMER_KEY", "consumer_key"); | |
define("CONSUMER_SECRET", "consumer_secret"); | |
define("OAUTH_TOKEN", "access_token"); | |
define("OAUTH_SECRET", "access_secret"); | |
function oauth_gen($method, $url, $iparams, &$headers) { | |
$iparams['oauth_consumer_key'] = CONSUMER_KEY; | |
$iparams['oauth_nonce'] = strval(time()); | |
$iparams['oauth_signature_method'] = 'HMAC-SHA1'; | |
$iparams['oauth_timestamp'] = strval(time()); | |
$iparams['oauth_token'] = OAUTH_TOKEN; | |
$iparams['oauth_version'] = '1.0'; | |
$iparams['oauth_signature'] = oauth_sig($method, $url, $iparams); | |
print $iparams['oauth_signature']; | |
$oauth_header = array(); | |
foreach($iparams as $key => $value) { | |
if (strpos($key, "oauth") !== false) { | |
$oauth_header []= $key ."=".$value; | |
} | |
} | |
$oauth_header = "OAuth ". implode(",", $oauth_header); | |
$headers["Authorization"] = $oauth_header; | |
} | |
function oauth_sig($method, $uri, $params) { | |
$parts []= $method; | |
$parts []= rawurlencode($uri); | |
$iparams = array(); | |
ksort($params); | |
foreach($params as $key => $data) { | |
if(is_array($data)) { | |
$count = 0; | |
foreach($data as $val) { | |
$n = $key . "[". $count . "]"; | |
$iparams []= $n . "=" . rawurlencode($val); | |
$count++; | |
} | |
} else { | |
$iparams[]= rawurlencode($key) . "=" .rawurlencode($data); | |
} | |
} | |
$parts []= rawurlencode(implode("&", $iparams)); | |
$sig = implode("&", $parts); | |
return base64_encode(hash_hmac('sha1', $sig, CONSUMER_SECRET."&". OAUTH_SECRET, true)); | |
} | |
$headers = array("Host" => "http://api.tumblr.com/", "Content-type" => "application/x-www-form-urlencoded", "Expect" => ""); | |
$params = array("data" => array(file_get_contents("/path/to/file"), file_get_contents("/path/to/file")), | |
"type" => "photo"); | |
$blogname = "testing.tumblr.com"; | |
oauth_gen("POST", "http://api.tumblr.com/v2/blog/$blogname/post", $params, $headers); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_USERAGENT, "PHP Uploader Tumblr v1.0"); | |
curl_setopt($ch, CURLOPT_URL, "http://api.tumblr.com/v2/blog/$blogname/post"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
"Authorization: " . $headers['Authorization'], | |
"Content-type: " . $headers["Content-type"], | |
"Expect: ") | |
); | |
$params = http_build_query($params); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
$response = curl_exec($ch); | |
print $response; | |
?> |
Excellent. Thanks!!!
How do I get "access_token", "access_secret"?
hey i also get error like Unauthorised...
how can i solve this?
Hi Great work..but the problem is .when am use the i can bale to post the image only..but i want post with text with image and some link also..an one help me
This works for me, as long as I hardcode the path to the files. When I try to automate this, I get an unauthorized response.
Why does this not work?
$images = array();
foreach ($tags as $tag) { array_push($images, file_get_contents( $tag->getAttribute('src') ) ); }
$params = array("data" => $images, "type" => "photo" );
BTW: I try to transfer the contents from a wordpress blog to tumblr.
I'm trying to pick out from this what the request looks like after you have done the OAuth dance. How do you use the tokens you get back? (I'm thinking in curl
terms)
Hi all, I've used this script for a while and everything runs smooth, but then I've changed hosting (a more limited one), and I started to notice some issues:
when adding more than 4Mb of images (photoset), I runs often out of memory, after debugging a little, I've noticed that every element of the $params array is rawurlencoded, that's nice, but urlencoding a 1Mb image results in a string of at least 3Mb, this is leading to out of memory errors. I've contacted the [email protected] and this is what they answer:
You shouldn't need to include the files in the parameters used for generating the oauth signature. For an example of how this is done, checkout one of our official API clients.
but, calling the oauth_gen() using a empty array, leads to 401 not authorized error...
do someone know something?
Hi!
This code is quite handy and worked for me.
But, can you tell me that how add caption(text) to images posting to tumblr.
I have tried this below
$params = array(
"data" => array(file_get_contents("path/to/image")),
"type" => "photo",
"generator" => "Testing tumblr API example"
);
But this wasn't working.
Secondly, can we add a link on the image while uploading it.
Waiting for the reply.
Thanks
Thanks for sharing that code, comment on the code will have been nice for beginners.
thanks
Dude, if 1 thumb-up means that I have to cut one bloody finger for you, I'm willing to cut all my fingers to represent my appreciation.
Everything posts and for that, **THANKYOU. But, it redirects me to the php script that I use to post the image and that is driving me crazy. I am guessing it has something to do with the callback url, but I can't seem to find a way to stop this behavior.