Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update_with_media using abraham's twitteroauth

I'm trying to implement an upload_with_media request from ajax using Abraham's twitteroauth library (TwitterOAuth v0.2.0-beta2). I've had no problems with basic posts but when I try to include media I get this as a response:

"{"request":"\/1\/statuses\/update_with_media.json","error":"Error creating status."}"

My code for posting media looks like this:

   $image = $_FILES["media"]["tmp_name"];

    $parameters = array(
        'media[]'  => "@{$image};type=image/jpeg;filename={$image}",
        'status'   => $status
      );

    if(isset($reply_id)) {
        $parameters['in_reply_to_status_id'] = $reply_id;
    }
    $post = $twitteroauth->post('https://upload.twitter.com/1/statuses/update_with_media.json', $parameters);
    echo json_encode($post);

I have verified that all data is being sent to this script correctly and even managed to get an update_with_media post working using the same data above and the tmhOAuth library but as the rest of my widget uses twitteroauth, I'd prefer to keep things uniform. I've also tried it with and without the .json affixed to the ending and saw no difference. Can anyone show me an example of a successful implementation of update_with_media using twitteroauth? I can't seem to figure out what I'm doing wrong.

like image 827
Mulberry Avatar asked May 10 '12 08:05

Mulberry


2 Answers

After dealing during hours for a solution to UPDATE_WITH_MEDIA with twitteraouth library, I found the following solution that works fine:

  • First: the PHP original library linked from Twitter Dev here does not work.

IS NOT WORKING WITH UPDATE_WITH_MEDIA

  • After searching and searching, I found the same library but with a fix to do it. You can find it here: https://github.com/tomi-heiskanen/twitteroauth/tree/77795ff40e4ec914bab4604e7063fa70a27077d4/twitteroauth

The basic diference is that the original has the function "post" without "$multipart" parameter. This parameter is what allows to send what Twiiter ask for in the documentation: a multipart enctype post. So at the end the basic code is as follows:

$image_path="folder/image.jpg";

$handle = fopen($image_path,'rb');
$image  = fread($handle,filesize($image_path));
fclose($handle);

$params = array(
  'media[]' => "{$image};type=image/jpeg;filename={$image_path}",
  'status'  => "Put your message here, must be less than 117 characters!"
);
$post = $connection->post('statuses/update_with_media', $params, true);

IMPORTANT! If you try this code with the original library, you will find out an error. You have to download from the link above and replace both files (OAuth.php and twitteroauth.php) in your project.

like image 144
Pedro Aguayo Avatar answered Sep 18 '22 14:09

Pedro Aguayo


Try using codebird-php https://github.com/mynetx/codebird-php

It turn out that it does the trick despite being last in the list of php libraries suggested by Twitter. Just grab codebird.php and cacert.pem from the git repo.

    include_once('codebird.php');
    \Codebird\Codebird::setConsumerKey($consumer_key, $consumer_secret);
    $cb = \Codebird\Codebird::getInstance();
    $cb->setToken($token, $token_secret);
    $status = 'Gamo, I just tweeted with an image!';
    $filename = '/home/asdf/test.png';
    $cb->statuses_updateWithMedia(array('status' => $status, 'media[]' => $filename));
like image 42
gtsouk Avatar answered Sep 20 '22 14:09

gtsouk