Skip to content

Instantly share code, notes, and snippets.

@codingjester
Created January 20, 2012 22:09

Revisions

  1. codingjester revised this gist Jun 9, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions upload.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    <?php

    #Requires PHP 5.3.0

    define("CONSUMER_KEY", "consumer_key");
    define("CONSUMER_SECRET", "consumer_secret");
    define("OAUTH_TOKEN", "access_token");
  2. codingjester revised this gist Jan 21, 2012. 1 changed file with 15 additions and 6 deletions.
    21 changes: 15 additions & 6 deletions upload.php
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ function oauth_gen($method, $url, $iparams, &$headers) {
    $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) {
    @@ -33,19 +33,28 @@ function oauth_sig($method, $uri, $params) {
    $iparams = array();
    ksort($params);
    foreach($params as $key => $data) {
    $iparams[]= rawurlencode($key) . "=" .rawurlencode($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" => file_get_contents("/path/to/file"), "type" => "photo");
    $params = array("data" => array(file_get_contents("/path/to/file"), file_get_contents("/path/to/file")),
    "type" => "photo");

    $blogname = "yourblog.tumblr.com";
    $blogname = "testing.tumblr.com";
    oauth_gen("POST", "http://api.tumblr.com/v2/blog/$blogname/post", $params, $headers);

    $ch = curl_init();
    @@ -66,4 +75,4 @@ function oauth_sig($method, $uri, $params) {

    $response = curl_exec($ch);
    print $response;
    ?>
    ?>
  3. codingjester created this gist Jan 20, 2012.
    69 changes: 69 additions & 0 deletions upload.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    <?php

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

    $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) {
    $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" => file_get_contents("/path/to/file"), "type" => "photo");

    $blogname = "yourblog.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;
    ?>