Mark Dalby

Fighting Ignorance since 1986 (It’s taking longer than I thought).

Advertisement

<?php
# @purpose                                                              To send direct messages on Twitter.
# @param                $to                                           Who you want the message to go to.
# @param                $message                                The content of the message.
# @param                $username                             The username of the twitter user to send the message from.
# @param                $password                               The password of the twitter user to send the message from.
# @returns                                                                Nothing.
# @changelog        15/03/2008                           Mark Dalby              function declare complete.
# @docend
function twitter_send_dm($to, $message, $username, $password)
{
define(’POSTURL’, “http://$username:$password@twitter.com/direct_messages/new.json”);
define(’POSTVARS’, “user=$to&text=$message”);
$ch = curl_init(POSTURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, POSTVARS);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
exit;
}
?>

The below function will allow you to send a DM to a Twitter user (assuming you are there friend). There is no success / failure checking with this function (I’m not going to make it too easy for you.)

<?php

# @purpose                                                              To send direct messages on Twitter.

# @param                $to                                           Who you want the message to go to.

# @param                $message                                The content of the message.

# @param                $username                             The username of the twitter user to send the message from.

# @param                $password                               The password of the twitter user to send the message from.

# @returns                                                                Nothing.

# @changelog        15/03/2008                           Mark Dalby              function declare complete.

# @docend

function twitter_send_dm($to, $message, $username, $password)

{

define(’POSTURL’, “http://$username:$password@twitter.com/direct_messages/new.json”);

define(’POSTVARS’, “user=$to&text=$message”);

$ch = curl_init(POSTURL);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, POSTVARS);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_exec($ch);

exit;

}

?>

Comments

There are 2 comments for this post.

  1. Polprav on October 16, 2009 10:29 pm

    Hello from Russia!
    Can I quote a post in your blog with the link to you?

  2. admin on October 16, 2009 11:57 pm

    Of course you can :-)

Write a Comment