Neena Vivek Neena Vivek - 2 months ago 14
PHP Question

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

I am posting on Tumblr using its API. 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:

$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

thrown in
/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 i.e.
Multiple
. I think that's because the associative array itself has comma separated values.

Answer

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. In that case, the tags property will be an array of 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

And as can you see there, it describes the request parameter tagsas follows:

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


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.

A string with comma-separated tags is simply that - a string:

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