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





thanks Sachin for this big share I bookmarked and tweeted
pls tell should i put this code in web host and how to put the twitter user and password in (twitter DM class)
thanks a lot man
Hi Karem,
All you have to do is replace the following with your username & password.
$credit=array(0=>”username”,1=>”password”);
That should do the trick.
Hi Sachin
Any word on getting this script going again with the new authorisation ?
Thanks
Hey Rob,
Its just that i have been lazy, but it will be soon up in a week or two .. Subscribe to the comments, that will notify you automatically when its there
Thanks for holding on !
Sachin Khosla
I am very interested in a follow up for oauth.
Hi sachin it seems not working
yes, because twitter has withdrawn its support for HTTP authentication.
I am releasing an OAUTH class soon, i know I have been saying this from quite a long time, but very soon
waiting…
Having error using this script..
i.e.
No cURL data returned for user=navinsharma_sr&text=navinsharma_sr [401]
Unable to send direct messages to navinsharma_sr
Doesn’t work for me, I got “Unable to send direct messages to xxx”
Any idea? Thanks!
Yes dude, because it has to use OAuth API