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

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

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA