用Tumblr API上传图片

tumblr-api-codeigniter-library

听说Tumblr在国内貌似解禁了(还是神壕告诉我的),他的API看起来算是比较健全,不过真到好好研究的时候发现,果然还是渣浪的比较好用。然后想吐槽一下点点,果然是照抄Tumblr的……做了好几个DEMO,发现文字没问题,发图片URL也没问题,就是无法上传图片……在谷歌娘上找了许久,也找了好多教程,发现看了好多篇都搞不懂……于是废话也不多说了,放上源码才是正道

config.php:

<?php
/**************************************************
 #  Tumblr-uploader &copy;  kagami.me
 #  config.php  Created on 2013.08.06
 #  Weibo: http://weibo.com/614520789
***************************************************/
//你的博客地址,可以是自定义域名
blogname = "spoonwep.tumblr.com";api_key = "GqxrJaz7dJHEubpTEsmUeM5d0m8NYpTVWnArGbP9pyCjigv3gm";
?>

tumblr.class.php:

<?php
/**************************************************
   #  tumblr-uploader &copy;  kagami.me
   #  tumblr.class.php  Created on 2013.08.06
   #  Weibo: http://weibo.com/614520789
***************************************************/
define("CONSUMER_KEY", "GqxrJaz7dJHEubpTEsmUeM5d0m8NYpTVWnArGbP9pyCjigv3gm");
define("CONSUMER_SECRET", "9BPnSEJZqOpiIgwySS8LKjUapkI4WFv9owUwY2xbDvYtZAgIIo");
define("OAUTH_TOKEN", "pSRVDYUpjnI4LS0kKkzxqR4UqIX4P1KmSOVCjFjDuEEOlDCgAI");
define("OAUTH_SECRET", "c8rX5T5bH4I2fcNOSEXTggbxoMUeJVGninB2E9DJiNo6VcWCBP");

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) {
            if(is_array(data)) {count = 0;
                foreach(data asval) {
                    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));
}
?>

upload.php:

<?php
/**************************************************
  #  tumblr-uploader &copy;  kagami.me
  #  upload.php  Created on 2013.08.06
  #  Weibo: http://weibo.com/614520789
***************************************************/
include('tumblr.class.php');
include('config.php');
function curl_data(url,data){
    curl = curl_init(); 
    curl_setopt(curl, CURLOPT_URL, url);
    curl_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1);
    curl_setopt(curl, CURLOPT_USERAGENT, _SERVER['HTTP_USER_AGENT']);
    curl_setopt(curl, CURLOPT_POST, 1);
    curl_setopt(curl, CURLOPT_POSTFIELDS,data);
    curl_setopt(curl, CURLOPT_TIMEOUT, 30);
    curl_setopt(curl, CURLOPT_HEADER, 0);
    curl_setopt(curl, CURLOPT_RETURNTRANSFER, 1);tmpInfo = curl_exec(curl);
    curl_close(curl);
    return tmpInfo;
}
 if(isset(_FILES['data']['tmp_name'])){
route=_FILES['data']['tmp_name'];

headers = array("Host" => "http://api.tumblr.com/", "Content-type" => "application/x-www-form-urlencoded", "Expect" => "");params = array("data" => array(file_get_contents("route"), file_get_contents("route")),"type" => "photo");

oauth_gen("POST", "http://api.tumblr.com/v2/blog/blogname/post",params, headers);ch = curl_init();
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);form = "api.tumblr.com/v2/blog/".blogname."/posts/photo?api_key=".api_key."";
var = '';json = curl_data(form,var);
form_url_info = json_decode(json,true);
//echo form."<br>";
echo '<img src="'.form_url_info["response"]["posts"]["0"]["photos"][0]["original_size"]["url"].'">';
//print $response;
}
?>

说明一下:

1、貌似Tumblr的API只能用文件流的方式上传,所以只能用file_get_contents的方式,把图片文件强制转换为字符串然后上传,所以导致的结果就是:不适合上传很大的图片。

2、本人PHP新手,对于这个程序,必须要php.ini中设置 register_globals = On,如果你看到这篇文章,希望能指导我一下怎么才能不使用这个注册全局变量的功能,感谢!另外,php版本5.2/5.3测试通过,其他没有测试,如果有条件可以自己测试一下
经测试,只要在index.php加上”session_start();”就行了……为此还头痛了好久,不过上传图片还是有可能失败,应该是file_get_contents函数的限制吧

放上DEMO:

https://sell.moe/demos/tumblr/index.php

这是测试图片存放的博客:

http://spoonwep.tumblr.com

代码托管到了Git:

https://github.com/spoonwep/tumblr-uploader

这篇文章还没有评论

发表评论

*

List
Love
00:00