5th Jul 2013 07:30am
1,957 views | 0 comments
Flattr this

So in the previous post in the Twitter Series, I covered how you can send a text-based tweet. Now in this post, I will show you how to send a tweet along with an image.

Now it should be noted at the moment that the PHP Twitter library we are using, Abraham, doesn't seem to support natively Twitter's "statuses/update_with_media" API call (neither did it in v1), however there are a few work arounds, some simple and some a bit complex, however for this tutorial we'll stick to a more simpler workaround. (Hopefully though in the not-too-distant-future, Abraham updates his stuff and allows it to work correctly).

So, before we start, in the folder we've been playing with all our twitter stuff in, create a new folder and call it something like "images". Inside that folder, put a JPG image in there (or quickly open up Paint and make a simple image and save it as a .jpg file). Now for this tutorial, I've saved my image as "hello.jpg", which you can see below... check out my excellent art skills and also a very noticeably low JPEG compression rate from Paint. Urgh.

Tweeting An Image

Now the image is done, head back to the normal folder and let's create a file called "image_tweet.php" (this will be the file that'll tweet a picture for you). Firstly we need to add our keys and secrets information (which you'll remember from the previous posts) along with including the class file and shoving it as a variable like so...

<?php // Consumer key $consumerKey = ''; // Consumer secret $consumerSecret = ''; // Access token $OAuthToken = ''; // Access token secret $OAuthSecret = ''; // Include Twitter OAuth Class include("twitteroauth/twitteroauth.php"); $tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret); ?>

Now we need to create a variable with the images location in it. (So below the "$tweet = new Twi.." line)

$filename = "./images/hello.jpg";

Where I put hello.jpg, you can change it to whatever you renamed the image as if you didn't use the same as me. Now we need the PHP to "open" the file as Twitter's "statuses/update_with_media" call only works with RAW data, so the URL to an image will mean nothing to it - it needs the actual images, erm, coding/binary data/blah blah... anyways, so yeah, you will need to open the image using this line (which will be familiar to you if you write text files or log files with PHP).

$binary = fopen($filename, "rb");

The "rb" in that line basically tells it to get ready to read the binary data of the file mentioned in $filename. Now the file's binary data is ready to be read, we need to read it, to do so, we do this...

$image = fread($binary, filesize($filename));

Now the $image variable is holding all the binary data from your image (the exact amount of it has been determined by the filesize() function - and because it's reading the same image, it's collecting the same amount of data). So now that $image has what we need to send, we can stop the $binary variable reading the file now using this.


The image is ready to be tweeted now, so, as we did yesterday, we will need to create our "post" tweet line, but instead of it being "statuses/update" and the "status" value in the array - like we did yesterday, we will be using "statuses/update_with_media" and "media[]". Like so...

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

Now if you run this, note: there may be a second or two of lag on the page loading, you will notice on your Twitter page that a picture has been posted. It should be the picture you made/used at the beginning of this tutorial.

Tweeting An Image

If you want to also add text to your tweet, so for example like this:

Tweeting An Image

Then in the "array()" bit add the "status" line from before (but remember to add a comma ( , ) after the "media[]" line!)

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

Until next time, enjoy!


Tweet ThisShare On FacebookShare On Google ReaderAdd To Del.icio.usShare On DiggShare On RedditShare On LinkedInShare On StumbleUpon


Post your comment...

Your Name:

Your Email:

Your Comment:

Are You Human?

Tick this box to confirm you're not a bot.

  • © Dale Hay, 2005 - 2016