Hey Folks,
In this post we are going to learn, How you can post picture to twitpic.com using the API services provided. It is very simple to send request to the API and post the picture on twitpic. You can also specify if you want to tweet about the picture to your twitter’s timeline or no. The following methods are provided by the API.

- METHOD: uploadAndPost (http://twitpic.com/api/uploadAndPost)
- METHOD: upload (http://twitpic.com/api/upload)
- Error codes
- 1001 – Invalid twitter username or password
- 1002 – Image not found
- 1003 – Invalid image type
- 1004 – Image larger than 4MB
Given above is the API description from twitpic’s API page. But you need not to get into those complicated details. Instead you can use a class which will let you post a picture on twitpic and update your twitter status on the same account. This is simple & easy to use. Check out the code below.
<?php class twitpic { /* * variable declarations */ var $post_url='http://twitpic.com/api/upload'; var $post_tweet_url='http://twitpic.com/api/uploadAndPost'; var $url=''; var $post_data=''; var $result=''; var $tweet=''; var $return=''; /* * @param1 is the array of data which is to be uploaded * @param2 if passed true will display result in the XML format, default is false * @param3 if passed true will update status twitter,default is false */ function __construct($data,$return=false,$tweet=false) { $this->post_data=$data; if(empty($this->post_data) || !is_array($this->post_data)) //validates the data $this->throw_error(0); $this->display=$return; $this->tweet=$tweet; } function post() { $this->url=($this->tweet)?$this->post_tweet_url:$this->post_url; //assigns URL for curl request based on the nature of request by user $this->makeCurl(); } private function makeCurl() { $curl = curl_init(); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); curl_setopt($curl, CURLOPT_URL, $this->url); curl_setopt($curl, CURLOPT_POST, 3); curl_setopt($curl, CURLOPT_POSTFIELDS, $this->post_data); $this->result = curl_exec($curl); curl_close($curl); if($this->display) { header ("content-type: text/xml"); echo $this->result ; } } private function throw_error($code) //handles few errors, you can add more { switch($code) { case 0: echo 'Think, you forgot to pass the data'; break; default: echo 'Something just broke !!'; break; } exit; } } //class ends here ?>
The above PHP class does all the trick to upload picture on twitpic and post status on the twitter. You can use this class with both using HTML form or using a PHP script to automatically upload the image and post the tweet. You can use the following HTML form and PHP script in combination to upload picture by taking
input from the user.
// This block of code should be written above the HTML and it will exit after the picture has been uploaded. If //you have turned display on (by passing 3rd param as true) then it will display the success message. if($_POST) { $file=$_FILES['media']; $postfields = array(); $postfields['username'] = $_POST['username']; $postfields['password'] = $_POST['password']; $postfields['message'] = $_POST['message']; $postfields['media'] = "@$file[tmp_name]"; $t=new twitpic($postfields,true,true); $t->post(); exit; }
<style type="text/javascript">
*{font-family:verdana;}
span{font-size:12px;color:#393939;}
h3{font-size:14px;color:#5AAAF7;}
</style>
<body>
<h3>Upload your pic to twitpic, and post status on twitter</h3>
<form method="post" enctype="multipart/form-data" action="<?= $_SERVER[PHP_SELF] ?>" >
<p><span style="height:40px;font-weight:bold;margin-right:56px;">Twitter Username :</span><input type="text" name="username" /></p>
<p><span style="height:40px;font-weight:bold;margin-right:61px;">Twitter Password:</span><input type="password" name="password" /></p>
<p><span style="vertical-align:text-top;height:40px;font-weight:bold;margin-right:28px;">Message to be posted :</span> <textarea cols="35" rows="2" name="message"></textarea></p>
<p><span style="vertical-align:text-top;height:40px;font-weight:bold;">Choose an image to upload: </span><input type="file" name="media" /></p>
<p style="width:250px;text-align:right;margin-top:50px;"><input type="submit" value="Upload »" /> </p>
</form>
<sup>Script powered by <a href="http://www.digimantra.com/">www.digimantra.com</a></sup>
</body>You can skip posting update to twitter by passing the third argument as false or just by skipping it. If you want to upload image programmatically, without the user input or the form then you can do it using the following code. Make sure the image path is correctly mention, else it will throw an error.
<?php $file='file_to_be_uploaded.gif'; $postfields = array(); $postfields['username'] = 'twitter_username'; $postfields['password'] = 'twitter_password'; $postfields['message'] = 'Message to be posted' ; $postfields['media'] = "@$file"; //Be sure to prefix @, else it wont upload $t=new twitpic($postfields,true,true); $t->post(); ?>
This above class posts the image in the binary format as discussed in Send binary data using curl php. You can change the class to suit your need and you can also enhance it the way you like. Hope this post helped you learn something.
Stay Digified!!
Sachin Khosla





[...] sharing twitpic API class with my blog visitors I have got a lot of visitors from Google which came to this blog searching [...]
Hi Sachin!
Thanks for these information.
Got a question regarding the twitpic api, is it possible to also retrieve pics related to a hashtag from varios users if they are published on twitter? How could this be done?
I know retrieving tweets that contains a choosen word or hastag is possible, but dont know how to do it with twitpic.
Thanks
Hey Humbucker,
As if now there is no such request that can be sent to twitpic servers.
Thanks
Sachin Khosla
[...] have already learnt “How to update your twitter status ?” and also learnt how to interact with twitpic API to successfully upload images. Today I am going to show you how you can send direct message on [...]
hi there,
is it possible to push images from a url to twitpic? id like to upload images from amazon s3 (rather than my local machine) for example.. many thanks,
vj
For a quick solution you can add one more step for that. Download the file locally using cURL, this has been explained in here.
Store the file locally or in a temp directory and then upload.
Thanks
Sachin Khosla
Hi Sachin,
Wanted to let you know that I’m using your class in a WordPress plugin that will upload images to Twitpic when you upload them to your blog.
Thanks for the great work! Saved me a few hours.
Best regards,
Byron
Forgot to mention, the plugin is called: twitpic-it
Cheers,
Byron
Just checked out your plugin, nice work
humm… the only thing in my screen is response:
–
XML Parsing Error: no element found
Location: http://localhost/test/twitpic.php
Line Number 1, Column 1:
^
–
May you help me ?
It’s probably the API is down. Otherwise are you sure you doing all the steps correctly.