update_with_media using abraham's twitteroauth

2020-07-08 07:40发布

问题:

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.

回答1:

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.



回答2:

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));


回答3:

The original library doesn't include uploading media functionality yet. You may check out https://github.com/natefanaro/twitteroauth .



回答4:

I suggest that you use Fiddler2 or some similar tool to examine and compare the messages that go out with twitteroauth, and also with tmhOAuth. You will see the difference.

In my experience, this is what an HTTP POST to Twitter looks like, using update_with_media.{xml,json}. The suffix you use affects only the respponse, I believe. (Your app must set the Authorization header in a way that is specific to your app.)

You want to get twitteroauth to post something like the following

POST https://upload.twitter.com/1/statuses/update_with_media.xml HTTP/1.1
Authorization: OAuth oauth_callback="oob", oauth_consumer_key="xxxxxxxxxxxx", oauth_nonce="7774328k", oauth_signature="pUYjRnccmrBYiO1j9cliETsw%2B5s%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1318300521", oauth_token="59152613-vrlZ2edX56PudQtBmpAWd3SPDt9cPyAhibO7ysl6W", oauth_version="1.0"
Content-Type: multipart/form-data; boundary=======c49479438c600bf59345e======
Host: upload.twitter.com
Content-Length: 7320
Connection: Keep-Alive

--======c49479438c600bf59345e======
Content-Disposition: form-data; name="status"

working on a Tweet tool that uses the OAuth Manager library.
--======c49479438c600bf59345e======
Content-Disposition: file; name="media[]"; filename="ThisIsAPicture.png"
Content-Type: image/png

  ...binary png data here...

--======c49479438c600bf59345e======--


回答5:

I want to sent url link with status param

like : Put you message here