statuses/update_with_media #137

Closed
wants to merge 1 commit into
from

Projects

None yet
@robhaswell

Hi there,

I made uploading images work as per https://dev.twitter.com/docs/api/1/post/statuses/update_with_media

You need to use $connection->upload() instead of $connection->post();

@leninZ
leninZ commented Jun 24, 2012

thanks rob, im try to make work your code. This is my request php. Plese helpme to now what is wrong.

$connection->upload('statuses/update_with_media',array(
'media[]' => '@/img/twitter_fugue.png',
'status' => 'test image post',
));

@scotepi
scotepi commented Sep 5, 2012

Please add this to master

@gavinwilliams

Would be good to get this into master, not supporting image uploads is a bit of a set back for this library

@Joeynoh
Joeynoh commented Nov 27, 2012

Great addition. It'd be nice to get some working examples up too

@aforsman

I was able to get this working using the code posted here:
themattharris/tmhOAuth#76 (comment)

@mrdekk
mrdekk commented Jan 10, 2013

This works for me, just need proper relative path to image and then working brilliant

$connection->upload('statuses/update_with_media',array(
'media[]' => '@img/twitter_fugue.png',
'status' => 'test image post',
));

@robhaswell

HI guys,

Sorry I have abandoned this ticket, but I no longer work in this industry and I don't have the time to maintain this patch. If someone wants to take over it I could offer help but it's quite a simple bit of code.

Best of luck,

-Rob

@obcidio
obcidio commented May 24, 2013

Hey folks, added a few methods to get this up and running. I don't have time to do more than paste it here, but here ya go:

EDIT:
I did modify some OAuth library method names so as not to clash with other libraries... FYI.

~ line 160

/**
   * POST wrapper for oAuthRequest.
   */
  function postWithMedia($url, $parameters = array()) {
    $response = $this->oAuthRequestWithMedia($url, 'POST', $parameters);
    if ($this->format === 'json' && $this->decode_json) {
      return json_decode($response);
    }
    return $response;
  }

~ line 180

 /**
   * Format and sign an OAuth / API request
   */
  function oAuthRequestWithMedia($url, $method, $parameters) {
    if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
      $url = "{$this->host}{$url}.{$this->format}";
    }

    $request = TwitterOAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url);
    $request->sign_request($this->sha1_method, $this->consumer, $this->token);
    // important! or you will get 403
    $oauthHeader = $request->to_header();
    log_message("API", "CRAP: $oauthHeader");
    switch ($method) 
    {
        case 'GET':
          return $this->http($request->to_url(), 'GET');          
          break;
        default:
              // important
          return $this->http($request->get_normalized_http_url(), $method, $parameters, $oauthHeader);
          break;
    }  
  }

~ line 230

/**
   * Make an HTTP request
   *
   * @return API results
   */
  function http($url, $method, $postfields = NULL, $oauth = FALSE) {

    $this->http_info = array();
    $ci = curl_init();
    /* Curl settings */
    curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
    curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
    //curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:')); old header options
    curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
    curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
    curl_setopt($ci, CURLOPT_HEADER, FALSE);
    curl_setopt($ci, CURLINFO_HEADER_OUT, true); // enable tracking
    curl_setopt($ci, CURLOPT_VERBOSE, true); // enable trackingCURLOPT_VERBOSE => TRUE,

    // important!
    $_h = array('Expect:');
    $_h[] = $oauth;

    curl_setopt($ci, CURLOPT_HTTPHEADER, $_h);

    switch ($method) {
      case 'POST':
        curl_setopt($ci, CURLOPT_POST, TRUE);
        if (!empty($postfields)) {
          curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
        }
        break;
      case 'DELETE':
        curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
        if (!empty($postfields)) {
          $url = "{$url}?{$postfields}";
        }
    }

    curl_setopt($ci, CURLOPT_URL, $url);
    $response = curl_exec($ci);
    $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
    $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
    $this->url = $url;

    curl_close ($ci);
    return $response;
  }

and lastly, to call, instantiate library as normal, than call the new method:

$parameters = array('status' => $vars['statusUpdate'], 'media[]' => "@{$photo};type=image/jpeg;filename={$photo}");
$status = $connection->postWithMedia('statuses/update_with_media', $parameters);

PS: No warranty, but works for me - haven't tested with other methods, but works as of posting date with 1.1/statuses/update and 1.1/statuses/update_with_media

@alex88
alex88 commented Jun 3, 2013

No news about this?

@robhaswell

Sorry guys, I'm not working on this project any longer.

@agriz
agriz commented Jun 24, 2013

This library works for me well. https://github.com/themattharris/tmhOAuth
I tried to use abraham's library. While i was testing, it is missing the media upload feature.

tmhOAuth is working good and has good tutorial with examples.

@rudemex
rudemex commented Jun 27, 2013

(sorry my english is not very good..) I solved as follows...

/-------------------/

$msg= "This is a test text...";

$filename = "images/image.jpg";
$handle = fopen($filename, "rb");
$image = fread($handle, filesize($filename));
fclose($handle);

$array = array('media[]' => "{$image};type=image/jpeg;filename={$filename}", 'status' => $msg);
$result = $connection->post('statuses/update_with_media', $array);

@vensder
vensder commented Jul 1, 2013

Thanks 👍 rudemex!
Good work!

@davidpintos

Thanks @rudemex !!
Me ha servido mucho tu aporte!
Saludos desde Argentina y aguante mexico!
;)

@rudemex
rudemex commented Jul 17, 2013

(Y) vensder, davidpintos

De nada!

@rudemex
rudemex commented Jul 17, 2013

CLOSED!

@prabuguna

is this working well? for me its not please help me

@prabuguna You can use https://github.com/natefanaro/twitteroauth, it works. It's based on this commit but has some improvements like latest API endpoint.

@cromerog

Not working ... Any updates since API 1.1 ?

@maliayas
maliayas commented Oct 5, 2013

Hi all. https://github.com/natefanaro/twitteroauth works with the current Twittter API. It's based on @robhaswell 's work.

@peter-p2people

Thanks maliayas for pointing out the the forked twitterauch. I can vouch that it works! But make sure you use upload() instead of post as @robhaswell advised. And thanks @robhaswell for all your past efforts.

@robhaswell

No problem. It sounds like everybody should use https://github.com/natefanaro/twitteroauth now. Thanks @natefanaro.

@robhaswell robhaswell closed this Feb 17, 2014
@huevoncito

@obcidio Thanks for the solution. Nothing quite like a simple patch.

@abraham abraham locked and limited conversation to collaborators Dec 30, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.