Jump to content

Recommended Posts

Posted

I actually was reading a few posts over at chocolatesoftware.com (fshost site) and there was talk of a titter or Facebook feed. I am not sure of what it consists of or what but it may be worth a shot. There is really no other reason I see a Facebook or Twitter feed at the fshost site for any other reason than for VA use. I could be wrong and way out in left field here.

  • Administrators
Posted

You can code up a module that has the pirep_file hook, and then post to the twitter api. Their api is pretty easy, you just need a key, and it's 3/4 calls. They have examples in PHP of how to

  • Moderators
Posted

Im thinking i could modify this to suite,

<?php
/* ---------------------------------------- */
// Change these parameters with your Twitter 
// user name and Twitter password.
/* ---------------------------------------- */
$twitter_username ='yourTwitterUserName';
$twitter_psw ='yourTwitterPassword';
/* ---------------------------------------- */

/* Don't change the code belove
/* ---------------------------------------- */
require('twitterAPI.php');
if(isset($_POST['twitter_msg'])){
$twitter_message=$_POST['twitter_msg'];
if(strlen($twitter_message)<1){ $error=1; } else { $twitter_status=postToTwitter($twitter_username, $twitter_psw, $twitter_message); } }
/* ---------------------------------------- */ 
?>


Don't touch the rest of the code!

In insertTwitterMsg.php you have a form that you can reuse in your web projects:


<!-- Send message to Twitter -->
<?php 
if(isset($_POST['twitter_msg']) && !isset($error)){?>
<div class="msg"><?php echo $twitter_status ?></div>
<?php } else if(isset($error)){?>
<div class="msg">Error: please insert a message!</div>
<?php }?>
<p><strong>What are you doing?</strong></p>
<form action="insertTwitterMsg.php" method="post">
<input name="twitter_msg" type="text" id="twitter_msg" size="40" maxlength="140"/>
<input type="submit" name="button" id="button" value="post" />
</form>

  • Moderators
Posted

This seems more adaptable,

<?php 

/*   This class is compatible with >= PHP 5 only due to the use of: 
   SimpleXML:  http://uk2.php.net/simplexml 
               Used to parse XML feeds  
               SimpleXML is enabled by default. If it appears not to be, contact your host. 

   cURL is also required for this script to operate. http://uk.php.net/curl 

   The Twitter API documentation used to create this class can be found: 
   http://groups.google.com/group/twitter-development-talk/web/api-documentation 

   Written by Matt Jewell 
   http://mediascratch.com 
*/ 

class twitterAPI { 

   public  $twitter_base         = 'http://twitter.com/statuses/'; 
   private $twitter_username     = 'your username'; 
   private $twitter_password     = 'your password'; 



   public function fetch_latest_status() { 

       /*  user_timeline 
           Returns the 20 most recent statuses posted in the last 24 hours from the authenticating user.  It's also possible to request another user's timeline via the id parameter below.         

           Parameters used: id  
                            count   
       */ 

       $buffer = file_get_contents($this -> twitter_base . 'user_timeline/' . $this -> twitter_username . '.xml?count=1'); 
               // Grab the contents of the XML file and store it in the variable. 

       $xml    = new SimpleXMLElement($buffer); 
               // Creating a new XML string for use with the SimpleXML extension. 

       $status_item = $xml -> status; 
               // Creating a new variable using SimpleXML with "status" as the Node. 
       return $status_item -> text; 
               // Grab the current status as the Element. 
   } 



   function update_status($message){ 


       $curl = curl_init(); 

       curl_setopt($curl, CURLOPT_URL, $this -> twitter_base . 'update.xml?status=' . stripslashes(urlencode($message))); 
           // The variable could also be substituded by the use of CURLOPT_POSTFIELDS 

       curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
           // Enables us to return the result as a string instead of outputting to browser. 

       curl_setopt($curl, CURLOPT_POST, 1); 
           // From the Twitter API docs: "Request must be a POST". CURLOPT_POST ensures this./ 

       $username = $this -> twitter_username; 
       $pass = $this -> twitter_password; 

       curl_setopt($curl, CURLOPT_USERPWD, "$username:$pass"); 
           // Authenticates the user (you) in the post request.    


       $exec = curl_exec($curl); 
           // Execute the cURL request. 

       // echo curl_error($curl);  
       // Used to debug cURL. 

       $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
           // Return the HTTP headers so we can confirm the request. 
           // A list of what the status codes are and mean can be found here: 
           // http://www.seoconsultants.com/tools/headers.asp#code-200     

       curl_close($curl); 
           // Terminate the cURL session 

       return ($httpcode == 200) ? 'Updated!' : 'Error!'; 
           /* Ternary operator equivelant to: 

              IF httpcode equals 200  
                      Return done 
              ELSE  
                      Return error     
           */ 

   } 
} 
?>

  • Moderators
Posted

OK so this works a treat,

/*
* Script that posts multiple RSS feeds to twitter
*
* Use this with crontab (it works with CLI too)
* to post your feed's content to twitter
*
* A tutorial by:
* http://www.webdigity.com/
*
*/

$settings ['twitter-username'] = 'YOUR USERNAME';// Your twitter username
$settings ['twitter-password'] = 'YOUR PW';// Your twitter password
$settings ['feed-url'] ='YOURURL/lib/rss/latestpilots.rss';//The url of the feed we are going to post to twitter or latestpireps.rss

set_time_limit(0);//We need this because otherwise php will probably time out
//Let's see if we have the curl library
if (!extension_loaded('curl') && !dl('curl.' . (stristr(php_OS, 'WIN')?'dll':'so'))) die( 'Curl is not loaded' );
//Now we need a file to log the last entry so we wont post it again
$file = dirname(__FILE__) . '/feed.log.txt';
if ( !file_exists( $file ) ){
  $fp = @fopen( $file, 'w');
  if (! $fp )
     die( 'Could not write to log file. Check your permissions.' );
  fclose( $fp );
} else
  if ( !is_writable( $file ) ) die( 'Could not write to log file. Check your permissions.' );
//Fetch the RSS feed
$ch = curl_init( $settings['feed-url'] );
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$feed = simplexml_load_string ( curl_exec($ch) );
curl_close($ch);
//Parse the feed
$messages = array();//Here we will store all the messages that we are going to post to twitter
foreach( $feed->channel->item as $item ){
  $title = $item->title; // If you want to fetch the description instead use $item->description
  $url = $item->link;
  //Have we already posted that?
  if ( $url == file_get_contents($file) )   break;
  //Now do the actual posting.
  //First we need to shorten the url
  $ch = curl_init( 'http://twt.gs/?url='.$url.'&user='.$settings['twitter-username'].'&pass='.$settings['twitter-password'] );
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $shortURL = curl_exec($ch);
  $resp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);
  if ( $resp != '200' )
     die('Problem with the url shortening service. Check back later. Error: '.$shortURL);
  //Now Calculate the twit message
  $cnt = 140 - strlen($shortURL) - 1;
  $messages [] = strlen($title) <= $cnt ? $title . ' ' . $shortURL : substr($title,0,$cnt-3).'... '.$shortURL;
  $lastURL = empty($lastURL) ? $url : $lastURL;
}
//Post to twitter
while( $message = array_pop($messages) ){
  $ch = curl_init('http://twitter.com/statuses/update.xml');
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, 'status='.urlencode($message));
  curl_setopt($ch, CURLOPT_USERPWD, $settings['twitter-username'].':'.$settings['twitter-password']);

  $response = curl_exec($ch);
  $resp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);
  if ( $resp != '200' )
     die('Problem with twitter. We should try later. Twitter reported: '.$response);
  else
     sleep(5);//Sleep 5 seconds before the next update
}
$fp = fopen($file, 'w');
fwrite($fp, $lastURL);
fclose($fp);
?>

Just need to put this is to a listener for a pirep filed and a new user registered.

  • 11 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...