如何使用 API 和 PHP 将多个标签添加到 Tumblr 中的 post?

How to add multiple tags to a post in Tumblr using the API with PHP?

我正在 post 使用 Tumblr API。我可以成功 post 并将单个标签添加到任何 post 但我在添加多个标签时遇到问题。

这是他们在指南中的建议:

{
"response": {
"posts": [
     {
        "blog_name": "blogname",

        "type": "text",
        "tags": [
           "tumblrize",
           "milky dog",
           "mini comic"
        ],
        "title": "A title",
        "body": "Some text for the post"
     },
     ...
  ]
 }
}

我在 PHP 中执行以下操作:

$data = array('title' => 'My Title', 'body' => 'My Content', 'tags' => 'A Single Tag');
$client->createPost($blog_n, $data);

为了使用多个标签,我编写了以下代码:

$data = array('title' => 'My Title', 'body' => 'My Content', 'tags' => array('A', 'Single', 'Tag'));

但这会引发异常。

任何人都可以帮助我了解如何 post 多个标签。

这是我得到的异常:

PHP Fatal error: Uncaught Tumblr\API\RequestException: [500]: Internal Server Error

thrown in /home/freadioc/public_html/socialmedia/tumblr/lib/Tumblr/API/Client.php

Link to the documentation.

更新

根据下面答案中的建议,我使用了以下代码:

$data = array('title' => 'My Title', 'body' => 'My Content', 'tags' => 'Multiple', 'Tags'));

使用上面的代码只会导致一个标签附加到 post,即 Multiple。我认为这是因为关联数组本身具有逗号分隔值。

This is what they suggest on their guide:

不,不是。这就是 JSON 数据结构,当您 读取 post 时,您可以期望从 API 返回 信息。在这种情况下,tags 属性 将是一个 数组 标签。

不过,您想在此处 制作 post,这意味着正确的文档部分是 https://www.tumblr.com/docs/en/api/v2#posting

如您所见,它对请求参数tags的描述如下:

tags | String | Comma-separated tags for this post

所以你必须传递一个字符串,而不是数组


This is the exception I get:

这也不是真的...

不,这不是您遇到的异常,这是您遇到的致命错误,因为您忽略了 捕获异常。

您应该继续阅读如何使用 try/catch 正确处理异常,以及如何查看它们实际包含的错误信息:http://php.net/manual/en/language.exceptions.php


编辑:

After the suggestion in the answer below, I used the following code:

  $data = array('title' => 'My Title', 'body' => 'My Content',
                'tags' => 'Multiple', 'Tags'));

没有人建议。

您没有在此处为键 tags 添加两个值,而只添加了一个 (Multiple) - 然后是一个完全独立的新数组元素,值为 Tags

带有逗号分隔标签的字符串就是 - 一个字符串:

$helloIAmAString = 'with,comma,separated,tags';