【发布时间】:2016-08-26 15:08:30
【问题描述】:
我需要遍历我在 Tumblr 上的所有现有帖子并向每个帖子(一个 URL)添加文本,但 Tumblr 大众编辑器只允许大量添加标签。
如何使用 API 来做我需要做的事情(好像没有这样使用 API 进行编辑的例子)?
PHP 或 Ruby 都可以。
【问题讨论】:
我需要遍历我在 Tumblr 上的所有现有帖子并向每个帖子(一个 URL)添加文本,但 Tumblr 大众编辑器只允许大量添加标签。
如何使用 API 来做我需要做的事情(好像没有这样使用 API 进行编辑的例子)?
PHP 或 Ruby 都可以。
【问题讨论】:
不,没有。 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 */
}
【讨论】: