Tweet Images Via API Using Codebird-PHP

posted Monday, March 23rd 2015 at 8:37 AM by

Tweet Images with Codebird-PHP

I wrote up a little tutorial earlier on how to tweet automatically using PHP, but I didn't cover how to Tweet media and images using the same API. Well here it is, a couple code snippets on how to tweet images using Codebird-PHP. Enjoy!

Initial Codebird-PHP Setup

This tutorial builds on the information posted in the earlier Codebird-PHP post, so make sure you've got Codebird-PHP set up and working properly before you continue.

Tweeting Images with Codebird-PHP

Once you've got Codebird up and running, you can modify the function from the previous tutorial to allow images to be uploaded. The biggest change is we connect with the API to upload an image to Twitter using $cb->media_upload().

After the image is posted to twitter, we can tweet using a message and an the image ID for the file we just uploaded. That's done with a slight modification to the parameters sent to $cb->statuses_update().

Have a look at the code:

function tweet($message,$image) {
	// add the codebird library
	// note: consumerKey, consumerSecret, accessToken, and accessTokenSecret all come from your twitter app at https://apps.twitter.com/
	\Codebird\Codebird::setConsumerKey("consumerKey", "consumerSecret");
	$cb = \Codebird\Codebird::getInstance();
	$cb->setToken("accessToken", "accessTokenSecret");

	//build an array of images to send to twitter
	$reply = $cb->media_upload(array(
		'media' => $image
	//upload the file to your twitter account
	$mediaID = $reply->media_id_string;
	//build the data needed to send to twitter, including the tweet and the image id
	$params = array(
		'status' => $message,
	  	'media_ids' => $mediaID
	//post the tweet with codebird
	$reply = $cb->statuses_update($params);
//example call
tweet("Autopost images to #Twitter with a #PHP API, #codebird-php check it out here: http://bit.ly/1OgKw3L","http://www.adamkonieska.com/images/tweet-images-with-codebird-php.png");

Advanced Codebirding

The Codebird API is very flexible and you can do some pretty neat things with it. For example, if you have a bunch of images to upload you can pass an array with all the images and upload them in one API call. That, and some other nice examples, are available on the Codebird-PHP GitHub page.

That's all for now, happy tweeting!

Share This:



View (8) Comments Post a Comment
  • Replying to Adam Konieska on Tweet Images Via API Using Codebird-PHP

  • sin
    Sunday, August 16th 2015 at 5:10 PM

    What is $image? Is it the logical path for the image on the server? Is it the URL from the browser? Is it the absolute path?

    • Replying to Sin

    • Adam Konieska
      Sunday, August 16th 2015 at 7:11 PM

      Hey Sin, $image is the full URL of the image. Twitter grabs the image from that URL and re-hosts it on its servers.

      Check out the example call at the bottom of the code snippet.

      Hope that helps!

      • Replying to AK

  • ron nuberg
    Ron Nuberg
    Wednesday, August 24th 2016 at 5:45 AM

    Adam, did you already try adding meta data to an image, I have tried media/metadata/create calling codeBird->media-metadata-create, but the function is not found. I am trying to upload a single picture and I believe it needs to be a multiple picture setup for this to wotk? What do you think?

    • Replying to Ron Nuberg

    • ron
      Wednesday, August 24th 2016 at 6:56 AM

      I found it, added and extra item to the POST array for this method.
      Picture got posted, but I saw no tags, so hunting on ;-)

      • Replying to ROn

  • shirish
    Friday, January 27th 2017 at 7:54 AM

    Is it possible to tweet with only Image URL in case I have only image cdn URL

    • Replying to Shirish

    • Adam Konieska
      Friday, January 27th 2017 at 12:52 PM

      Of course! Just use the CDN url in the function call.

      • Replying to AK

  • bob
    Saturday, March 11th 2017 at 9:11 AM

    This is a great help! Thanks so much for posting this. I was using Twitter OAuth REST API Library, then I did a google search for a simple way to post media to Twitter and found this, which is how I found out about codebird! It seems so simple and lightweight. I really like it!

    I have a question though. I am trying to post video, an MP4 to twitter. I used the basic example code for video posting included in codebird and used your idea of turning it into a function like you did here for this image upload example.

    The one thing the function is missing is some error handling so the function returns the error message that twitter replied with, or true if no errors were returned, and a tweet could be posted with the attached video. Any idea on how to best accomplish this? Do you already have a video upload codebird example post somewhere on your blog that I missed maybe? Do you have other examples using codebird in functions with error handling? Thanks a lot for the examples and all the help you are providing! AWESOME!

    • Replying to Bob

  • piotr
    Monday, June 12th 2017 at 8:39 AM

    I have a problem with this message, please help, hint what I can do. Thank you.

    Notice: Undefined property: stdClass::$media_id_string
    stdClass Object ( [errors] => Array ( [0] => stdClass Object ( [code] => 89 [message] => Invalid or expired token. ) ) [httpstatus] => 401 [rate] => )

    • Replying to Piotr