doucao1888 2013-09-23 18:05
浏览 69
已采纳

从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!

  • 写回答

1条回答 默认 最新

  • doupo2241 2013-09-23 21:34
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了