Embed URL


SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Working PHP example of uploading a photo with V2 api

View upload.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#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;

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.


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?

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.


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.

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

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

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

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

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

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

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

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!

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

IHDRddG<efPLTE���U����etc. etc. etc.
                                             [1] => �PNG

IHDRddp��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.


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


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

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

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

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


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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.