Update_with_media using abraham twitteroauth

I am trying to fulfill the upload_with_media request from ajax using the Abraham twitteroauth library (TwitterOAuth v0.2.0-beta2). I had no problems with basic messages, but when I try to turn on the media, I get this as an answer:

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

My code for publishing materials is as follows:

  $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 checked that all data is sent to this script correctly and even managed to get the update_with_media message working using the same data above and the tmhOAuth library, but since the rest of my widget uses twitteroauth, I would prefer everything to be consistent. I also tried it with and without .json attached to the finale and saw no difference. Can someone show me an example of successful implementation of update_with_media using twitteroauth? I cannot understand what I am doing wrong.

+7
source share
5 answers

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

It turns out that he does the trick, despite the fact that he is the last on the list of php libraries offered by Twitter. Just grab codebird.php and cacert.pem from the git repository.

  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)); 
+4
source

After working for an hour to solve the UPDATE_WITH_MEDIA with the twitteraouth library, I found the following solution that works fine:

  • First up: the original PHP Dev library associated with Twitter Dev does not work here .

DOES NOT WORK WITH UPDATE_WITH_MEDIA

The main difference is that the original has a "post" function without the "$ multipart" parameter. This parameter allows you to send a Twiiter request in the documentation: multipart enctype message. Therefore, at the end, the base 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 source library, you will find an error. You should download from the link above and replace both files (OAuth.php and twitteroauth.php) in your project.

+4
source

The source library does not include downloading multimedia features yet . You can check https://github.com/natefanaro/twitteroauth .

+2
source

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

In my experience, that is exactly what HTTP POST for Twitter looks like using update_with_media. {XML, JSON}. I suppose the suffix you use only affects the answer. (Your application should set the authorization header in a way that is specific to your application.)

You want 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======-- 
0
source

I want to send url link with param status

like: put a message here

0
source

All Articles