+ Post New Thread
Results 1 to 3 of 3
Web Development Thread, altering twittermailphp script in Coding and Web Development; Trying to alter a php script so that instead of sending status updates sends direct message updates. Code: #!/usr/local/bin/php -q ...
  1. #1

    russdev's Avatar
    Join Date
    Jun 2005
    Location
    Leicestershire
    Posts
    6,919
    Thank Post
    708
    Thanked 550 Times in 365 Posts
    Blog Entries
    3
    Rep Power
    204

    altering twittermailphp script

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

    Code:
    #!/usr/local/bin/php -q
    <?php
    ##########################################################################
    # MailTwitterPHP: Update Twitter via email
    #
    # Version: 0.2
    # Date:    January 16, 2007
    # Author:  Scott Jarkoff <http://jarkolicious.com/>
    # 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 "<h1>Headers in INBOX</h1>\n";
    	$headers = imap_headers($mbox);
    	echo "<code>";
    	print_r($headers);
    	echo "</code><br /><br />";
    
    	if ($headers == false) {
    		echo "Call failed<br />\n";
    	} else {
    		foreach ($headers as $val) {
    			echo $val . "<br />\n";
    	   }
    	}
    }
    
    # Cycle through the mailbox and send new mail as an update to Twitter.
    if ($debug == 1) {
    	echo "<h1>Mailbox Contents</h1>\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, "<a><br><br />");
    	$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 . "<br />\n";
    			echo "Subj: " . $subject . "<br /><br />\n\n";
    			echo nl2br($message) . "<br /><br />\n\n";
    		}
    		
    		# Update Twitter via Basic Authorization
    		$twitter_message = $twitter_update . $message;
    		if ($debug == 1) {
    			echo $twitter_message . "<br /><br />\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 ...<br />\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

  2. #2
    My220x's Avatar
    Join Date
    Feb 2008
    Location
    UK
    Posts
    257
    Thank Post
    9
    Thanked 3 Times in 3 Posts
    Blog Entries
    8
    Rep Power
    14
    why do you need to have

    Code:
    $twitter_update = "http://twitter.com/direct_messages/new.xml?user=russdev_ping&text=";
    wouldn't it be:
    Code:
    $twitter_update = "http://twitter.com/direct_messages/new.xml?user=russdev&text=";

  3. #3

    russdev's Avatar
    Join Date
    Jun 2005
    Location
    Leicestershire
    Posts
    6,919
    Thank Post
    708
    Thanked 550 Times in 365 Posts
    Blog Entries
    3
    Rep Power
    204
    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

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 1
    Last Post: 2nd July 2008, 04:20 PM
  2. Altering music information
    By FN-GM in forum General Chat
    Replies: 4
    Last Post: 27th December 2007, 10:47 PM
  3. Best script ever!
    By Zabu in forum Scripts
    Replies: 0
    Last Post: 6th November 2007, 11:48 PM
  4. Logon script & Printer script question.
    By Galway in forum Windows
    Replies: 3
    Last Post: 29th August 2007, 11:00 AM
  5. Does anyone have a script...
    By wesleyw in forum Scripts
    Replies: 4
    Last Post: 5th July 2007, 01:58 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •