Search code examples
phpimagetwitter

Twitter API tells me " media type unrecognized." for images that always worked


Edit 2020-28-02: It was an error on Twitter's side (thanks for the useful comment). I keep the question up in spite of the down votes because it could happen again, and this could be useful for others with the same error "media type unrecognized".

I have a small app, me as the sole user, updating my thematic twitter daily. By pressing a button it posts one much smaller than 5MB jpg image every day from a pool of 12. It worked for months like this. Now Twitter tells me: "media type unrecognized."

This is one of the images.

Symptoms:

  • I can't upload the same image to Twitter on the website either. So I assume my images got banned, from uploading.
  • I can upload png version of the same image to the website but not by my PHP software. And that is strange.
  • I can upload a different image both from my PHP script and on Twitter website.

I use this class for my PHP script

Only error in my log is this:

[22-Jan-2020 08:45:37 UTC] PHP Notice: Undefined property: stdClass::$media_id_string in /home/ABC/public_html/XYZ.php on line 205

It simply means: the upload was not successful, so Twitter API did not respond with a media ID (at this point it is NULL)

This is my script (around this there is logic to decide the text and for logging me in):

require_once 'twitteroauth/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;

define('CONSUMER_KEY', 'my consumer key');
define('CONSUMER_SECRET', 'my consumer secret');
define('ACCESS_TOKEN', 'my access token');
define('ACCESS_TOKEN_SECRET', 'my access secret');

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

$postfields = array(
    'media' => base64_encode(file_get_contents($image))
);

$media = $connection->upload('media/upload', ['media' => $image]);

$parameters = [
            'status' => 'my tweet text, never longer than 200 chars'
            'media_ids' => $media->media_id_string
];

$result = $connection->post('statuses/update', $parameters);

I repeat: this worked without a problem for months, I did not change anything in it. It just started to post the text without the image.

I realize there are several other questions about similar problem with similar error message out there, but non of the solutions worked for me.

  • I regenerated my access token and secret: nothing changed
  • I tried chunked upload: same result
  • I made new jpg files with 1 pixel smaller dimension so I trick Twitter it might be another image: it did not trick twitter
  • I tried to sleep(1) after the upload line: that was un-logical and silly on my part (but I thought might be the upload was slow, as one of the folks having the same problem with javascript version solved it like this)

Solution

  • Twitter's documentation states:

    Ensure the POST is a multipart/form-data request. Either upload the raw binary (media parameter) of the file, or its base64-encoded contents (media_data parameter). Use raw binary when possible, because base64 encoding results in larger file sizes

    However it looks like you are uploading base64-encoded contents, while using the media parameter. Try changing the media parameter to media_data.

    It's likely a recent change or one that is only recently enforced, hence it working before for you.