Howdy People,
You 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 twitter using the Twitter API and PHP. This piece of code will allow you to send not only single direct message (DM) at a time but also you can send direct message to multiple user all at once. Please note that using the web interface of twitter you can send DM to only one user at a time.
For those who do not want to mess with this API thing and think this API, PHP words are alienated for them, then there is a web application which allows you to do the similar task. TweetGuru is a web application which uses twitter’s secure OAUTH to send multiple direct messages. All you have to do is just authenticate this application and you are good to send multiple DMs.
There may be instances when you need to send multiple direct messages, for example you might want to send some invite for a party to some particular friends on twitter, then you can write their twitter handles in the comma separated form to send the same invite message, as show in the image below.
As you can see in the image, this application has a limit to send direct message to 12 users at a time. But if you use the following code with all the pieces on its right place, then you can send it to more than 12 users at a time.
To successfully send the multiple direct messages, download the twitter DM class file and include it in your PHP script. Please note that you have to save the file as PHP file to include it in your PHP script file. I have kept the name MultiDM.php for this example. You can give any name to the file but the class name should remain the same as MultiDM, as long as you do not change the contents of the file.
<?php require_once('MultiDM.php'); if(!empty ($_POST)) { $credit=array(0=>"username",1=>"password"); //credential array $api = new MultiDM($credit); //pass the credentials $users = explode("," , $_POST['friends']); // create an array from the csv foreach($users as $user) { $sent = $api->dm(trim($user),$_POST['msg']); // send the direct message to array of users if($sent === false) $failed [] = $user; // if not sent push into the failed array else $success [] = $user; } if(count($failed) > 0) // display a list of user to which message is not sent { echo "Unable to send direct messages to <strong>".implode(", ", $failed)."</strong>"; $api->output(); } if(count($success) > 0) { echo "Direct Messages successfuly sent to <strong>".implode(", ", $success)."</strong>"; $api->output(); } unset ($api,$success,$failed,$credit,$users,$user); // in the end unset all the vars } ?> <!-- HTML to display form which send multi DM --> <style> label {font-weight: bold;display: block;} </style> <h3> Send Multi-DM to your friends </h3> <form action="" method="post" > <p> <label for="friends">Friends</label><input type="text" value="" name="friends" id="friends" /> </p> <p> <label for="msg">Direct Message to be sent:</label><textarea rows="5" cols="25" name="msg" id="msg" ></textarea> </p> <input value="Send DM" type="submit" /> </form> |
So in the above example, you have a HTML form similar to what you see at TweetGuru. Provide the comma separated names of twitter handles and the message, hit submit and the direct messages will be sent.
Hope that helps !!
Stay Digified !!
Sachin Khosla