<?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; | |
?> |
This comment has been minimized.
This comment has been minimized.
Yep I just cloned the gist, added my secret and the blog that I wanted to upload to and it worked! What version of PHP are you using? |
This comment has been minimized.
This comment has been minimized.
Ah ha, looks like it doesn't work on PHP5.2.9 (at least) which is what the machine that was running the script is using. Having switched to php5.3.0 on it, the script runs correctly. |
This comment has been minimized.
This comment has been minimized.
Excellent :) I'll update the gist to reflect the needed version of PHP. Thanks for the interest and update :) Let me know if you need anything. Hit me on twitter, same handle as github. |
This comment has been minimized.
This comment has been minimized.
doesn't work for me, gives me 401 unauthorized. PHP 5.3.1 xampp on win, curl 7.19.6 I know my keys and secrets are correct, because they work with https://gist.github.com/1242662 |
This comment has been minimized.
This comment has been minimized.
@Vivec Strange. it's working for me. Just ran it. Did you make sure to set your tumblelog? |
This comment has been minimized.
This comment has been minimized.
This works with one image, but fails (401) with multiple images. Any ideas? |
This comment has been minimized.
This comment has been minimized.
What do you mean by this? "Did you make sure to set your tumblelog?" Also, how are you getting these? define("OAUTH_TOKEN", "access_token"); Are you just running a separate auth script and saving the token & secret? Thanks for sharing... |
This comment has been minimized.
This comment has been minimized.
Hi, I need to post text or Images on tumblr using PHP. |
This comment has been minimized.
This comment has been minimized.
@mpaler, you get those tokens when you complete a 3-legged oauth authentication. They give let your app act on behalf of the user who is currently using your application. |
This comment has been minimized.
This comment has been minimized.
@duotrigesimal i'd need more information. it's most definitely working on my end for multiple photos, you just replace the path in the "file_get_contents" calls to be the path to the image you want. http://zomgomgomgomg.tumblr.com/post/36366086988 Was just posted with it. |
This comment has been minimized.
This comment has been minimized.
Christ, I was banging my head against a wall trying to base64 encode the result of |
This comment has been minimized.
This comment has been minimized.
Hm…yeah I’m starting to see problems with this on multiple images as well, @duotrigesimal—did you make any progress? I thought it might have been memory related, but even when I try with relatively small files, it starts to go haywire. http://lol.kevinsweeney.me/post/47231238835 and
@codingjester if you need more info, I’m happy to provide. |
This comment has been minimized.
This comment has been minimized.
Hello, I downloaded your script and ran it on php version 5.3.8 using xampp 1.7.7 I used the OAuth Client and i'm getting the Token and Secret Token. I used the same token in the above script. I keep getting 401, Not Authorised error. could you please help me figure it out?? Thanks in Advance |
This comment has been minimized.
This comment has been minimized.
Hi! Thank you very much for your simple and workable code! |
This comment has been minimized.
This comment has been minimized.
Hi, My question: http://stackoverflow.com/q/18470624/2151050 qO6vhj97GmeMdkZagAZvilJqIa0 = My problem is "qO6vhj97GmeMdkZagAZvilJqIa0" which changes everytime I tried $response = curl_exec($ch); $json_response = json_decode($response); print $json_response->response[0]->id; The return response was qO6vhj97GmeMdkZagAZvilJqIa0= (example because the json array name changes every time) So How I can print only 59496435804 from that response |
This comment has been minimized.
This comment has been minimized.
Hey Adam, Once you have a response, and you json_decode it, you should be able to access it like a hash. So for API response it will be: print $json_response['response']['id'] If you post more code, I probably can get you through this :) |
This comment has been minimized.
This comment has been minimized.
@codingjester This php example works perfectly for images but not for uploading audio files.I try find the reason but i can't. When i change form enctype to multiform it says 'Not Authorized' and when i leave it as in the example it says 'Bad Request'. Do you know what should i change in the code in order to work? Thanks |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
Excellent. Thanks!!! |
This comment has been minimized.
This comment has been minimized.
How do I get "access_token", "access_secret"? |
This comment has been minimized.
This comment has been minimized.
hey i also get error like Unauthorised... |
This comment has been minimized.
This comment has been minimized.
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 comment has been minimized.
This comment has been minimized.
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. BTW: I try to transfer the contents from a wordpress blog to tumblr. |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
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:
but, calling the oauth_gen() using a empty array, leads to 401 not authorized error... do someone know something? |
This comment has been minimized.
This comment has been minimized.
Hi!
But this wasn't working. Secondly, can we add a link on the image while uploading it. Waiting for the reply. |
This comment has been minimized.
This comment has been minimized.
Thanks for sharing that code, comment on the code will have been nice for beginners. thanks |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
Hmm, does this gist still work? Trying my credentials in it, it always fails with 401 Unauthorised... using your python photo uploader Gist, it seems to work correctly.