Tumblr API reference の和訳

http://lostage.is-a-geek.org/api/tumblr/

ありがたい。(^_^
ただし、サンプルが Ruby で書き直されている。


元のドキュメントには PHP で書かれたサンプルが載っている。とはいえ、photo のアップロードのサンプルコードはない。

Tumblr API reference(英文)


Google Code Search で、 python だけどちょっと参考になりそうなコードを見つけた(tumblr.py)。

これを見る限り、普通に multipart/form-data で POST するコードがあれば、それをちょこっといじるだけでよさそう。


cURL というライブラリを使えば簡単にできるようだ。レンタルサーバーの phpinfo を調べると、cURL support enabled なので cURL は使えそう。(^_^

libcurl examples の中のこれとかを参考にすればいいんだろう。
The multipartpost.php Example


ちなみに、cURL を使わずにやる方法もあるようだが、このサンプルじゃデータを送ってないように見えるのだが(cURL を使わずに、PHP で POST リクエストを送信する)。



結局、http://www.php.net/manual/ja/ref.curl.php のこのコードがほぼ完璧では?これをまねれば、jpeg ファイルは同時にいくつでも送れるだろう(とりあえず1つで十分なのだけど)。

The examples below for HTTP file upload work great, but I wanted to be able to post multiple files through HTTP upload using HTML arrays as specified in example 38.3 at

http://us3.php.net/features.file-upload

In this case, you need to set the arrays AND keys in the $post_data, it will not work with just the array names. The following example shows how this works:

<?php

    $post_data = array();
   
    $post_data['pictures[0]'] = "@cat.jpg";
    $post_data['pictures[1]'] = "@dog.jpg";
   

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://my.domain.com/my_url.php" );
    curl_setopt($ch, CURLOPT_POST, 1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $postResult = curl_exec($ch);

    if (curl_errno($ch)) {
       print curl_error($ch);
    }
    curl_close($ch);
    print "$postResult";
?>