将内容发布到Tumblr

时间:2012-02-03 13:03:14

标签: php tumblr

我已经尝试了下面的代码,

function upload_content(){
    // Authorization info
    $tumblr_email    = 'email-address@host.com';
    $tumblr_password = 'secret';
    // Data for new record
    $post_type  = 'text';
    $post_title = 'Host';
    $post_body  = 'This is the body of the host.';
    // Prepare POST request
    $request_data = http_build_query(
        array(
            'email'     => $tumblr_email,
            'password'  => $tumblr_password,
            'type'      => $post_type,
            'title'     => $post_title,
            'body'      => $post_body,
            'generator' => 'API example'
        )
    );
    // Send the POST request (with cURL)
    $c = curl_init('api.tumblr.com/v2/blog/gurjotsinghmaan.tumblr.com/post');  
    //api.tumblr.com/v2/blog/{base-hostname}/post       
    //http://www.tumblr.com/api/write       
    //http://api.tumblr.com/v2/blog/{base-hostname}/posts/text?api_key={}
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($c);
    $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
    curl_close($c);
    // Check for success
    if ($status == 201) {
        echo "Success! The new post ID is $result.\n";
    } else if ($status == 403) {
        echo 'Bad email or password';
    } else {
        echo "Error: $result\n";
    }
}

1 个答案:

答案 0 :(得分:0)

您需要使用正确的Tumblr端点:

http://www.tumblr.com/api/write

我很确定其他人不会工作。

显然确保你的用户和传递是正确的,除此之外,这看起来很好 - 这几乎就是我写的。