wazzaday
wazzaday

Reputation: 9664

uploading media to the twitter API

I am trying to upload media to twitter via the API but I keep getting 'bad request' errors

HTTP request failed! HTTP/1.1 400 Bad Request

Here is my call

$image = 'http://4.bp.blogspot.com/-JOqxgp-ZWe0/U3BtyEQlEiI/AAAAAAAAOfg/Doq6Q2MwIKA/s1600/google-logo-874x288.png';

$media_ids = $twitterService->request('https://upload.twitter.com/1.1/media/upload.json', 'POST', array(
    'media' => $image
));

I am authenticating with oauth and any other type of request seems to work such as getting a users timeline.

I am also using the libary - https://github.com/Lusitanian/PHPoAuthLib - which appends things like the oauth token for me. Any ideas?

EDIT:

I have also attempted to make the same request with a base64 image like so:

$path = '../uploads/0PQ4j0yfMl1420641234.jpg';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

$media_ids = $twitterService->request('https://upload.twitter.com/1.1/media/upload.json', 'POST',     array(
        'media_data' => $base64
    ));

and by grabbing the raw image

$image = file_get_contents($path);

$media_ids = $twitterService->request('https://upload.twitter.com/1.1/media/upload.json', 'POST', array(
        'media' => $image
    ));

Upvotes: 1

Views: 8300

Answers (2)

vishalknishad
vishalknishad

Reputation: 744

For image urls you can do something like this

$image = 'http://4.bp.blogspot.com/-JOqxgp-ZWe0/U3BtyEQlEiI/AAAAAAAAOfg/Doq6Q2MwIKA/s1600/google-logo-874x288.png';

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

Upvotes: 0

Dmitry Sadakov
Dmitry Sadakov

Reputation: 2158

As the docs say - the image should be the base64 encoded bytes of the image, not a URL to the image.

Upvotes: 1

Related Questions