-
-
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; | |
?> |
@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
�
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/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!!!
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.
Hi, I need to post text or Images on tumblr using PHP.
Then Do you have any idea how to post the above stuff?