Cody S
Cody S

Reputation: 87

Retrieving Posts from Tumblr

I've tried to do my research regarding this topic but the more digging I do, the more confused I'm getting - seems like the documentation on Tumblr's API is all over the place (and fairly dated?); so perhaps someone out there will have some advice for me.

All I'm trying to do is to snag my most recent Tumblr posts and display them on the main page of my website; nothing too fancy. I don't need to POST to tumblr, I just need the info; the idea is that the team I'm working with would like to use Tumblr as a sort of poor-man's CMS while maintaining their presence on the Tumblr site proper.

I can easily retrieve posts using PHP and SimpleXML (http://username.tumblr.com/api/read) - but this doesn't offer too much support outside simple text posts (Other content, like video, photosets, etc - post differently); and it doesn't really seem like an officially supported method of accessing tumblr data (or..maybe it is?). Tumblr itself points folks towards tumblr.php, but I for the life of me can't figure out how to set it up since there's no real documentation for someone just starting to dabble. Some posts on StackOverflow say Tumblr.php doesn't work anymore and point folks to tumblrOauth.php, but I'm running into the same problem. No real 'new to API' documentation. I know how to set up my 'app' in Tumblr - I just don't know how all the Github files (for both of these systems) communicate to each other/the page...

Anyone have any advice? I'm just looking for a point in the right direction, maybe an initial nudge on how to hook the bits up?

Much thanks e-friends!

Upvotes: 1

Views: 2727

Answers (1)

GroovyCarrot
GroovyCarrot

Reputation: 514

Hey I've used a few API's but not the Tumblr API, after reading through this page: http://www.tumblr.com/docs/en/api/v2 I'm pretty sure I understand how to set this up so I'll have a go at explaining and you can let me know if you have some success

(Removed code, that PHP API is awful)

EDIT: Original edit removed

Right since the Tumblr PHP Client is rubbish I've had a look myself and just designed a little snippet of code to solve the problem here and retrieve your latest posts in an array that will give you like so:

Array
(
    [0] => Array
        (
            [id] => 49867777007
            [type] => photo
            [description] => 
            [date] => 2013-05-07 18:28:41 GMT
            [short_url] => http://tmblr.co/Z5XfMxkSMUFl
            [caption] => 
            [photo_url] => http://31.media.tumblr.com/a13494fb3cda1e40ab39211973a094f9/tumblr_mm0038sZGH1qdlh1io1_400.gif
            [width] => 395
            [height] => 350
        )

Use this code and then go through your $myposts later on the page

<?php
    $apikey = "your_api_key";
    $limit = 10;
    $tumblr = "your.tumblr.com";

    $apidata = json_decode(file_get_contents("http://api.tumblr.com/v2/blog/$tumblr/posts?api_key=$apikey&limit=$limit"));

    $mypostsdata = $apidata->response->posts;
    $myposts = array();

    $i = 0;
    foreach($mypostsdata as $postdata) {
        $post['id'] = $postdata->id;
        $post['type'] = $postdata->type;
        $post['description'] = $postdata->description;
        $post['date'] = $postdata->date;
        $post['short_url'] = $postdata->short_url;
        $post['caption'] = $postdata->caption;

        $post["photo_url"] = $postdata->photos[0]->original_size->url;
        $post["width"] = $postdata->photos[0]->original_size->width;
        $post["height"] = $postdata->photos[0]->original_size->height;

        $myposts[$i] = $post;
        $i++;
    }

    // Then handle $myposts later on your page
    echo '<pre>'.print_r($myposts).'</pre>';

    // There's quite a lot more data you can use so if you want
    // to print out $mypostsdata then you might find any more data you need
?>

Sorry the previous suggestions didn't work, the PHP Client really is awful but let me know if that's enough or if you need some revision to it

Upvotes: 8

Related Questions