Jump to content

Recommended Posts

Posted

Trying to alter a php script so that instead of sending status updates sends direct message updates.

 

#!/usr/local/bin/php -q
##########################################################################
# MailTwitterPHP: Update Twitter via email
#
# Version: 0.2
# Date:    January 16, 2007
# Author:  Scott Jarkoff 
# URL:	   http://jarkolicious.com/projects/mail-twitter/
#
# License: Creative Commons Attribution-NonCommercial-ShareAlike 2.5
#          http://creativecommons.org/licenses/by-nc-sa/2.5/
#
# Notes:   1) This is a work in progress. MailTwitterPHP will currently
#          connect to a specified server via IMAP and use all email
#          in the INBOX on the server as the basis for a new Twitter
#          post. There is minimal "security" added in that the subject
#          line of the email must match one of the settings.
#          2) PHP *MUST* have IMAP compiled in otherwise this script
#          will not function.
#
# Plans:   Add the ability to use POP3 in addition to IMAP.
#
# Disclaimer: All attempts have been made to ensure this functions as 
#             described above. In the event that something goes awry, the
#             author is not to be held liable. There is no guarantee that
#             this script will not cause you to lose your girlfriend,
#             run up your electric bill, delete all files on your server,
#             etc... Use *COMMON SENSE* for crying out loud. :-)
#
# 0.2:     Modified the script so that it uses POST, as it should have
#          been using from day 1.
##########################################################################

# -- USER DEFINED SETTINGS -----------------------------------------------

$imap_server	= "mail.russdev.com";
$imap_port	= "143";				// 143 for IMAP
$imap_user	= "email";
$imap_password	= "*****";

// The following variable and the subject line of the email must match
// in order for the email to be posted to Twitter.
$imap_security	= "****";

$twitter_user	= "russdev";
$twitter_pass	= "*****";

# -- DO NOT EDIT ANYTHING FURTHER ----------------------------------------

$twitter_update = "http://twitter.com/direct_messages/new.xml?user=russdev_ping&text=";
$imap_security	= strtolower($imap_security);
$imap_mailbox	= "{" . $imap_server . ":" . $imap_port . "}";
$imap_type		= "IMAP";

# Login to the specified IMAP server.
$mbox = imap_open("{" . $imap_server . ":" . $imap_port . "/imap/notls}INBOX", $imap_user, $imap_password);
if (!$mbox) {
die("Error connecting to the mail server ...\n");
}

# If there are no messages on the server then quit.
$mbox_info = imap_status($mbox, "{" . $imap_server . ":" . $imap_port . "/imap/notls}INBOX", SA_MESSAGES);
if ($mbox_info) {
if ($mbox_info->messages <= 0) {
	echo "There are no messages awaiting on the server ...\n";
	imap_close($mbox);
	exit;
}
}

if ($debug == 1) {
echo "Headers in INBOX\n";
$headers = imap_headers($mbox);
echo "";
print_r($headers);
echo "

";

if ($headers == false) {
	echo "Call failed
\n";
} else {
	foreach ($headers as $val) {
		echo $val . "
\n";
   }
}
}

# Cycle through the mailbox and send new mail as an update to Twitter.
if ($debug == 1) {
echo "Mailbox Contents\n";
}
for ($i = 1; $i <= imap_num_msg($mbox); $i++) {
$struct		= imap_fetchstructure($mbox, $i);
$header		= imap_headerinfo($mbox, $i, 80, 80);
$subject	= strtolower($header->fetchsubject);
if (($struct->type == 1) && (sizeof($struct->parts) == 0))
	$message	= imap_fetchbody($mbox, $i, "1");
else
	$message	= imap_body($mbox, $i);
$message = trim(substr($message, 0, 154));
$message = strip_tags($message, "

");
$message = urlencode($message);

# IF there are actual characters in the message AND
# IF the subject of the email matches the subject specified in the options above
# THEN attempt to post the message to Twitter via their XML update URL.
if(strlen($message) > 0 && $subject == $imap_security) {
	if ($debug == 1) {
		echo "From: " . $header->fromaddress . "
\n";
		echo "Subj: " . $subject . "

\n\n";
		echo nl2br($message) . "

\n\n";
	}
	
	# Update Twitter via Basic Authorization
	$twitter_message = $twitter_update . $message;
	if ($debug == 1) {
		echo $twitter_message . "

\n\n";
	}
	
	$curl_twitter = curl_init();
	curl_setopt($curl_twitter, CURLOPT_URL, $twitter_message);
	curl_setopt($curl_twitter, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	curl_setopt($curl_twitter, CURLOPT_HEADER, false);
	curl_setopt($curl_twitter, CURLOPT_USERPWD, "$twitter_user:$twitter_pass");
	curl_setopt($curl_twitter, CURLOPT_POST, true);
	$curl_result = curl_exec($curl_twitter);
	curl_close($curl_twitter);
} else {
	echo "Message " . $i . "; ERROR: non-existent message or security subject string mismatch ...
\n";
}

imap_delete($mbox, $i);
}

imap_expunge($mbox);
imap_close($mbox);
?>

 

Api info is at api-documentation - Twitter Development Talk | Google Groups

 

Can anyone see what I am doing wrong must be something obvious...

 

Russ

Posted

why do you need to have

 

$twitter_update = "http://twitter.com/direct_messages/new.xml?user=russdev_ping&text=";

 

wouldn't it be:

$twitter_update = "http://twitter.com/direct_messages/new.xml?user=russdev&text=";

Posted

As russdev is my main account and then russdev_ping is a second account which use to have status and various other messages sent to me.

 

so russdev account is sending the message to russdev_ping.

 

Russ

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...