Search code examples
phpapitumblr

"Bad Request" response on Tumblr's API "quote" request


I develope a PHP script that uses Tumblr's API. When I post a "text" everything works fine, but on "quote" posts I get a "Bad Request" error. I use Composer to get the "tumblr/tumblr" repository from https://github.com/tumblr/tumblr.php.

This is my PHP script:

<?php

require 'vendor/autoload.php';

$consumer_key = 'KEY';
$consumer_secret = 'SECRET';
$token = 'TOKEN';
$token_secret = 'SECRET';
$blog = 'BLOG';

$client = new Tumblr\API\Client($consumer_key, $consumer_secret, $token, $token_secret);

#$options = array('type' => 'text', 'title' => 'Title', 'body' => 'Body', 'tags' => 'Test');
$options = array('type' => 'quote', 'text' => 'Text', 'source' => 'Source', 'tags' => 'Test');

try{
    $res = $client->createPost($blog, $options);
}
catch(Exception $e){
    print "ERROR: ".$e->getMessage()."\n";
    exit(1);
}

How can I fix this?


Solution

  • From the documentation, the quote type requires a "quote" field instead of a "text" field.

    Change your parameters to this and it should work:

    $options = array('type' => 'quote', 'quote' => 'Text', 'source' => 'Source', 'tags' => 'Test');
    

    Granted, it looks like the Tumblr\API\RequestException doesn't return a helpful error message. I was able to figure this out by hacking Tumblr\API\RequestException and putting a var_dump in the constructor:

    public function __construct($response)
    {
        $error = json_decode($response->body);
        var_dump($error->response);
        ...
    

    With that, I was at least able to get a good error message:

    class stdClass#668 (1) {
      public $errors =>
      array(1) {
        [0] =>
        string(21) "Post cannot be empty."
      }
    }