Skip to content

Instantly share code, notes, and snippets.

Created January 20, 2012 22:09
  • Star 22 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Working PHP example of uploading a photo with V2 api
#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();
foreach($params as $key => $data) {
if(is_array($data)) {
$count = 0;
foreach($data as $val) {
$n = $key . "[". $count . "]";
$iparams []= $n . "=" . rawurlencode($val);
} 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" => "", "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 = "";
oauth_gen("POST", "$blogname/post", $params, $headers);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "PHP Uploader Tumblr v1.0");
curl_setopt($ch, CURLOPT_URL, "$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;
Copy link

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.

Copy link

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?

Copy link

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.

Copy link

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.

Copy link

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

Copy link

@Vivec Strange. it's working for me. Just ran it. Did you make sure to set your tumblelog?

Copy link

This works with one image, but fails (401) with multiple images. Any ideas?

Copy link

mpaler commented Nov 18, 2012

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");
define("OAUTH_SECRET", "access_secret");

Are you just running a separate auth script and saving the token & secret?

Thanks for sharing...

Copy link

jrvaja commented Nov 21, 2012

Hi, I need to post text or Images on tumblr using PHP.
Then Do you have any idea how to post the above stuff?

Copy link

@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.

Copy link

@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. Was just posted with it.

Copy link

Christ, I was banging my head against a wall trying to base64 encode the result of file_get_contents not realizing I could have just…stopped. You’re a hero, John!

Copy link

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. and work when posted separately, but when I try to send both their sources through (reading the sources from, I see this in my output:

OAuthException Object
    [message:protected] => Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)
    [string:Exception:private] => 
    [code:protected] => 401
    [file:protected] => /Users/kevinsweeney/Dropbox/Sites/
    [line:protected] => 77
    [trace:Exception:private] => Array
            [0] => Array
                    [file] => /Users/kevinsweeney/Dropbox/Sites/
                    [line] => 77
                    [function] => fetch
                    [class] => OAuth
                    [type] => ->
                    [args] => Array
                            [0] =>
                            [1] => Array
                                    [api_key] => wXrOUSzJyKWySzmfPS9Ht3jrwVtjWihK8TPsZmep36MFr1lqND
                                    [type] => photo
                                    [date] => 2013-04-06 00:00:34 GMT
                                    [caption] => 
                                    [data] => Array
                                            [0] => �PNG

IHDRdd��G<ef�PLTE���U����etc. etc. etc.
                                             [1] => �PNG

IHDRdd��p��T    pHYs����������iCCPPhotoshop ICC profilexڍ��  etc. etc. etc.


                            [2] => POST



    [previous:Exception:private] => 
    [lastResponse] => {"meta":{"status":401,"msg":"Not Authorized"},"response":[]}
    [debugInfo] => Array
            [sbs] =>  /* omitted for brevity */
            [headers_sent] => POST /v2/blog/ HTTP/1.1
User-Agent: PECL-OAuth/1.2.3
Accept: */*
Authorization: OAuth

Content-Length: 47988
Content-Type: application/x-www-form-urlencoded
            [headers_recv] => HTTP/1.1 401 Not Authorized
Date: Sat, 06 Apr 2013 00:05:37 GMT
Server: Apache
Vary: Accept-Encoding
X-Tumblr-Usec: D=353856
Content-Length: 60
Connection: close
Content-Type: application/json
            [body_sent] => /* omitted for brevity */
            [body_recv] => {"meta":{"status":401,"msg":"Not Authorized"},"response":[]}
            [info] => About to connect() to port 80 (#0)
Connected to ( port 80 (#0)
upload completely sent off: 47988 out of 47988 bytes
Closing connection #0



@codingjester if you need more info, I’m happy to provide.

Copy link


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

Copy link

emiklos commented Aug 17, 2013


Thank you very much for your simple and workable code!
It saw in days with other APIs, and your code working in just 5 minutes.
You are the best! :)

Copy link

Hi, My question:
I'm trying to return (print) only post id from the response using json_decode the main problem is that the json array name has a different name for each response

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

Copy link

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 :)

Copy link

@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?


Copy link

unsalted commented Dec 6, 2013

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.

Copy link

Excellent. Thanks!!!

Copy link

How do I get "access_token", "access_secret"?

Copy link

hey i also get error like Unauthorised...
how can i solve this?

Copy link

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 one help me

Copy link

olsp commented Feb 17, 2015

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.

Copy link

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)

Copy link

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 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?

Copy link

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.

Copy link

maxarch commented Sep 9, 2016

Thanks for sharing that code, comment on the code will have been nice for beginners.


Copy link

stonyau commented Feb 27, 2017

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.

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