O'Reilly

Twitter Direct Messaging Remotely Using cURL

By on  
Twitter DM

Twitter's direct messaging functionality is great. While simple tweeting communicates "just a clever quip," direct messaging communicates "listen here b*tch, this is important enough that you need to get this message NOW via text message." Twitter's API allows you direct message remotely so now you can let your website DM you (and call you a b*tch) any time you want!

The Shell cURL Command

curl -u davidwalshblog:myPass -d "text=Testing a remote direct message via C
URL&user=fellowTweeter" http://twitter.com/direct_messages/new.xml

This example shows the cURL command as you'd type it in the terminal.

PHP Usage

$result = shell_exec('curl -u davidwalshblog:myPass -d "text=Testing a remote direct message via C
URL&user=fellowTweeter" http://twitter.com/direct_messages/new.xml');
This is how you could use the command in PHP.

Sample XML Response

<!-- Successful! -->
<?xml version="1.0" encoding="UTF-8"?>
<direct_message>
  <id>186275699</id>
  <sender_id>15759583</sender_id>
  <text>Testing a direct message.</text>
  <recipient_id>15759583</recipient_id>
  <created_at>Sat Jun 20 00:13:41 +0000 2009</created_at>
  <sender_screen_name>davidwalshblog</sender_screen_name>
  <recipient_screen_name>davidwalshblog</recipient_screen_name>
  <sender>
    <id>15759583</id>
    <name>davidwalshblog</name>
    <screen_name>davidwalshblog</screen_name>
    <location>Madison, WI, US</location>
    <description>Flexile Programmer, MooTools Core Team Member, JavaScript Fanatic, CSS Tinkerer, PHP Hacker, and web lover.</description>
    <profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/57860553/footer-logo_normal.jpg</profile_image_url>
    <url>http://davidwalsh.name</url>
    <protected>false</protected>
    <followers_count>1834</followers_count>
    <profile_background_color>EEEEEE</profile_background_color>
    <profile_text_color>000000</profile_text_color>
    <profile_link_color>2A447B</profile_link_color>
    <profile_sidebar_fill_color>CDCDCD</profile_sidebar_fill_color>
    <profile_sidebar_border_color>999999</profile_sidebar_border_color>
    <friends_count>20</friends_count>
    <created_at>Thu Aug 07 04:18:53 +0000 2008</created_at>
    <favourites_count>3</favourites_count>
    <utc_offset>-21600</utc_offset>
    <time_zone>Central Time (US & Canada)</time_zone>
    <profile_background_image_url>http://s3.amazonaws.com/twitter_production/profile_background_images/15318223/twitter-background.png</profile_background_image_url>
    <profile_background_tile>false</profile_background_tile>
    <statuses_count>1734</statuses_count>
    <notifications>false</notifications>
    <verified>false</verified>
    <following>false</following>
  </sender>
  <recipient>
    <id>15759583</id>
    <name>davidwalshblog</name>
    <screen_name>davidwalshblog</screen_name>
    <location>Madison, WI, US</location>
    <description>Flexile Programmer, MooTools Core Team Member, JavaScript Fanatic, CSS Tinkerer, PHP Hacker, and web lover.</description>
    <profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/57860553/footer-logo_normal.jpg</profile_image_url>
    <url>http://davidwalsh.name</url>
    <protected>false</protected>
    <followers_count>1834</followers_count>
    <profile_background_color>EEEEEE</profile_background_color>
    <profile_text_color>000000</profile_text_color>
    <profile_link_color>2A447B</profile_link_color>
    <profile_sidebar_fill_color>CDCDCD</profile_sidebar_fill_color>
    <profile_sidebar_border_color>999999</profile_sidebar_border_color>
    <friends_count>20</friends_count>
    <created_at>Thu Aug 07 04:18:53 +0000 2008</created_at>
    <favourites_count>3</favourites_count>
    <utc_offset>-21600</utc_offset>
    <time_zone>Central Time (US & Canada)</time_zone>
    <profile_background_image_url>http://s3.amazonaws.com/twitter_production/profile_background_images/15318223/twitter-background.png</profile_background_image_url>
    <profile_background_tile>false</profile_background_tile>
    <statuses_count>1734</statuses_count>
    <notifications>false</notifications>
    <verified>false</verified>
    <following>false</following>
  </recipient>
</direct_message>



<!-- ERROR RESPONSE -->
<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <request>/direct_messages/new.xml</request>
  <error>You cannot send messages to users who are not following you.</error>
</hash>

Loads of information if your DM is successful -- a simple error response if your attempt fails.

So why automate a DM? I think one handy time to implement direct message automation is when you want to receive text messages when events occur on the website. Say you're a salesperson and you have a "request a phone call" form on your site. Since you're out of the office much of the day (driving), you don't have the means to check your email. Instead of setting up a text messaging / SMS system, simply set up a Twitter account and take advantage of Twitter's texting capabilities.

Would you ever want automated tweets or direct messages from your website?

Track.js Error Reporting

Recent Features

  • CSS 3D Folding Animation

    Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...

  • Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event,...

Incredible Demos

Discussion

  1. PHP has a curl implementation. Use that instead of doing a command execution.

  2. hmm, could be nice with a new mootwitter plugin, for direct messaging from own site, or twitting new blogs directly in a short version on twitter

  3. Andreson

    When I use PHP to login to facebook, it logs me out of facebook in my web-browser.

    If you use the same cookie jar as your webrowser in the script, then this shouldn’t happen. I’ve had trouble doing this with Safari, because cURL can’t parse the .plist format it uses for it’s cookie jar. I believe Firefox uses a format for it’s cookie jar that cURL can understand, even on a Mac.

    Also, it’s probably a good idea to only login if you have to. Facebook keeps you logged in for a very long time, but unfortunately does not support multiple logins. To tell if I’ve been logged out, I look at the of the page that curl gives me microsoft test.

    Personally I think facebook should support multiple logins, but unfortunately there’s nothing I can do about this.

  4. OK, Question…. is there a way to have twitter trigger a web script upon RECEIPT of a tweet? I would like to write a “tweet response” script that replies to codes sent in a tweet and would like it to be event driven, upon an incoming tweet, rather than something clumsy an inefficient like a cron job checking for new tweets every 30 seconds… is this possible to anyone’s knowledge?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • Follow Redirects with cURL

    I love playing around with cURL. There's something about loading websites via command line that makes me feel like some type of smug hacker, just like tweeting from command line does. I recently cURL'd the Google homepage and saw the following: I found it weird that Google...

  • Developers Have WordPress, Amateurs Have Squarespace, Professional Designers Have the NEW Webydo!

    Web design platforms have traditionally come in one of two varieties. There are the solutions like WordPress and Drupal that are incredibly powerful, but an understanding of web development and coding is required to be able to use those platforms effectively. On the other side of the...

  • Chris Coyierâs Favorite CodePen Demos II

    Hey everyone! Before we get started, I just want to say it’s damn hard to pick this few favorites on CodePen. Not because, as a co-founder of CodePen, I feel like a dad picking which kid he likes best (RUDE). But because there is just so...

  • GSAP + SVG For Power Users: Motion Along A Path

    Now that the GreenSock API is picking up steam, there are many tutorials and Getting Started guides out there to provide good introductions to the library, not to mention GreenSock’s own Forum and Documentation. This article isn’t intended for beginners, but rather a...

  • Copy a Directory from Command Line

    Copying a directory for the sake of backup is something I do often, especially when I'm trying to figure out why something isn't working when I use an external library.  I'll copy the directory structure as a backup, mess around with the original source until I find a solution,...