public
Last active

Working PHP example of uploading a photo with V2 api

  • Download Gist
upload.php
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
<?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;
?>

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 https://gist.github.com/1242662

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

http://zomgomgomgomg.tumblr.com/post/36366086988 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.

http://lol.kevinsweeney.me/post/47231238835 and
http://lol.kevinsweeney.me/post/47231216642 work when posted separately, but when I try to send both their sources through (reading the sources from http://kevinsweeney.me/post/47230868154), 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/swaplr.com/index.php
    [line:protected] => 77
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => /Users/kevinsweeney/Dropbox/Sites/swaplr.com/index.php
                    [line] => 77
                    [function] => fetch
                    [class] => OAuth
                    [type] => ->
                    [args] => Array
                        (
                            [0] => http://api.tumblr.com/v2/blog/lol-kevin-sweeney.tumblr.com/post
                            [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/lol-kevin-sweeney.tumblr.com/post HTTP/1.1
User-Agent: PECL-OAuth/1.2.3
Host: api.tumblr.com
Accept: */*
Authorization: OAuth
oauth_consumer_key="redacted",
oauth_signature_method="HMAC-SHA1",
oauth_nonce="716749111515f66d0e3e553.93628938",
oauth_timestamp="1365206736",
oauth_version="1.0",
oauth_token="redacted",
oauth_signature="redacted"

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
P3P: CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
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 api.tumblr.com port 80 (#0)
  Trying 66.6.36.17...
connected
Connected to api.tumblr.com (66.6.36.17) 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.

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

Hi!

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: http://stackoverflow.com/q/18470624/2151050
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 =
{
"meta":
{
"status":201,
"msg":"Created"
},
"response":
{
"id":59496435804
}
}

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?

Thanks

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.