TwitterOauth - upload single image with status text

php

#1

Hi,

Been using twitterauth to post text updates just fine.

Now i need to tweet a single image and text.

Searcehd for ages but can’t find a php example of how to tweet a single image and text … only found example using multiple images.

anyone show me how to use twitteroauth to tweet singe image ad text?


#2

Posting one image using TwitterOAuth is the same as posting multiple. You just change how many image files to include.

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret);
$media1 = $connection->upload('media/upload', ['media' => '/path/to/file/kitten1.jpg']);
$parameters = [
    'status' => 'Meow Meow Meow',
    'media_ids' => implode(',', [$media1->media_id_string]),
];
$result = $connection->post('statuses/update', $parameters);

#3

Thanks


#4

why is it returning Undefined property: stdClass::$media_id_string?

$connection = new TwitterOAuth($consumerkey, $consumersecret, $access_token, $access_token_secret);
$media1 = $connection->upload(‘media/upload’, [‘media’ => ‘/home/birdsey8/www/images1/twitter_image.png’]);
$parameters = [ ‘status’ => ‘Cartoon upload test’, ‘media_ids’ => implode(’,’,[$media1->media_id_string])];

$result = $connection->post(‘statuses/update’, $parameters);


#5

Notice: Undefined property: stdClass::$media_id_string in /home/birdsey8/php/twitter.php on line 40

Catchable fatal error: Object of class stdClass could not be converted to string in /home/birdsey8/php/twitter.php on line 44


#6

How do I retrieve the media_id?


#7

That looks correct @Robert_Seymour. I suspect that the upload request is failing and returning an error instead of an uploaded media response. Try throwing in some error handling before you post the tweet.

$media1 = $connection->upload(...);
if ($connection->getLastHttpCode() == 200) {
  // Post tweet
} else {
  // Handle error case
}

#8

Hello I would like to post a title a url a hashtag and an image, I get there but the url did not recognize (no click on it possible). Should we do something special?
thank you


#9

Make sure you include https:// a part of the URL.


#10

oops I’m sorry, in fact I sent the correct url but as it pointed to a local url (and local domain) it did not work, off course :wink: , thank you for your help and for your api


#11