简体   繁体   English

如何使用带有PHP的API在Tumblr中的帖子中添加多个标签?

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

I am posting on Tumblr using its API. 我正在使用其API在Tumblr上发布。 I can successfully post and add a single tag to any post but I am having trouble while adding multiple tags. 我可以成功发布一个标签并将其添加到任何帖子,但是添加多个标签时遇到了麻烦。

This is what they suggest on their guide: 这是他们在其指南中建议的内容:

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

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

I do the following in PHP: 我在PHP中执行以下操作:

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

To use multiple tags I wrote the following code: 为了使用多个标签,我编写了以下代码:

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

but that raises an exception. 但这引发了一个例外。

Can anyone please help me to understand how to post multiple tags. 任何人都可以帮助我了解如何发布多个标签。

This is the exception I get: 这是我得到的例外:

PHP Fatal error: Uncaught Tumblr\\API\\RequestException: [500]: Internal Server Error PHP致命错误:未捕获的Tumblr \\ API \\ RequestException:[500]:内部服务器错误

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

Link to the documentation . 链接到文档

UPDATE 更新

After the suggestion in the answer below, I used the following code: 在以下答案中提出建议之后,我使用了以下代码:

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

Using the code above only results in one tag attached to the post ie Multiple . 使用上面的代码只会在帖子上附加一个标签,即Multiple I think that's because the associative array itself has comma separated values. 我认为这是因为关联数组本身具有逗号分隔的值。

This is what they suggest on their guide: 这是他们在其指南中建议的内容:

No, it isn't. 不,不是。 That's the JSON data structure you can expect to get back from the API when you read post information. 这是您在阅读帖子信息时可以期望从API 取回的JSON数据结构。 In that case, the tags property will be an array of tags. 在这种情况下, tags属性将是标签数组

You want to make a post here however, and that means the correct documentation section is https://www.tumblr.com/docs/en/api/v2#posting 你想使一张贴在这里然而,这意味着正确的文档部分是https://www.tumblr.com/docs/en/api/v2#posting

And as can you see there, it describes the request parameter tags as follows: 如您所见,它描述了请求参数tags ,如下所示:

tags | 标签| String | 字串| Comma-separated tags for this post 此帖子的逗号分隔标签

So you have to pass a string , not an array . 因此,您必须传递一个字符串 ,而不是数组


This is the exception I get: 这是我得到的例外:

And that's not true either ... 而且也不是...

No, this is not the exception you got, it is the fatal error you get because you neglected to catch the exception. 不,这不是您所获得的异常,这是您由于忽略捕获该异常而导致的致命错误。

You should go read up on how to use try/catch to properly handle exceptions, and how to get a look at what error information they actually contain: http://php.net/manual/en/language.exceptions.php 您应该阅读有关如何使用try / catch来正确处理异常的信息,以及如何查看它们实际包含的错误信息: http : //php.net/manual/zh/language.exceptions.php


Edit: 编辑:

After the suggestion in the answer below, I used the following code: 在以下答案中提出建议之后,我使用了以下代码:

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

Nobody suggested that. 没有人建议。

You have not added two values for the key tags here, but only one ( Multiple ) - and then a completely independent new array element with the value Tags . 您在这里没有为键tags添加两个值,而仅添加了一个( Multiple )-然后添加了一个具有值Tags的完全独立的新数组元素。

A string with comma-separated tags is simply that - a string : 一个带有逗号分隔标签的字符串就是这样- 一个string

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM