【问题标题】:Tumblr API: Loop through all existing posts and add textTumblr API:遍历所有现有帖子并添加文本
【发布时间】:2016-08-26 15:08:30
【问题描述】:

我需要遍历我在 Tumblr 上的所有现有帖子并向每个帖子(一个 URL)添加文本,但 Tumblr 大众编辑器只允许大量添加标签。

如何使用 API 来做我需要做的事情(好像没有这样使用 API 进行编辑的例子)?

PHP 或 Ruby 都可以。

【问题讨论】:

    标签: php ruby tumblr


    【解决方案1】:

    不,没有。 API 不提供一次编辑所有帖子的方法。你必须一次做一个。

    您可以通过posts methodapi.tumblr.com/v2/blog/{blog-identifier}/posts?api_key={key} 检索您的帖子列表,一次最多可以返回 20 个帖子。您可以指定offset 参数,以继续拉入更多帖子,直到您到达末尾。

    $requestURI = "http://api.tumblr.com/v2/blog/$blogId/posts?api_key=$APIkey&offset=%d";
    if ($response = file_get_contents(sprintf($requestURI, 0))) {
        $data = json_decode($response);
        $posts = $data["response"]["posts"];
        $totalPosts = $data['total_posts'];
        $gotPosts = count($posts);
        while($gotPosts < $totalPosts) {
            $offset = $totalPosts - $gotPosts;
            $response = file_get_contents(sprintf($requestURI, $offset));
            $data = json_decode($response);
            $posts = array_merge($posts, $data["response"]["posts"]);
            $gotPosts = count($posts);
        }
    }
    

    一旦您为给定的 Tumblelog 积累了所有帖子的列表,您就可以遍历它们并通过 edit method api.tumblr.com/v2/blog/{blog-identifier}/post/edit 编辑每个帖子。

    foreach($posts as $post) {
        /* do your edit posts here */
    }
    

    【讨论】:

    • 我如何将 URL 添加到帖子正文?
    • 阅读答案中链接的 API 文档。您已经有了帖子正文,只需在此处连接您想要的链接并重新发送帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2015-07-13
    • 1970-01-01
    • 2014-02-03
    相关资源
    最近更新 更多