Multiple Instances of a feed on one page


#1

Hi

I have a client who wants to have the 3 tweets with in the main content of there page and then just there latest tweet displayed in the footer. I can make one feed work but not the other. I am quite new to this. So my question is, is this possible and how would I go about doing this. Any help would be much appreciated.

the plugin i have been using is include below, it works great for one instance, but i am not sure where to start to modify it for two

Cheers Keith

 <?php

class twitter_plugin {

	function __construct() {

		// Set your cache settings here...
		$this->cache = true;
		$this->cache_time = '1 min';
		$this->feed_cache = '/home/thekspot/public_html/test/assets/inc/twitter_cache/twitter_feed.php';   // absolute path to the file on the webserver

	}


	function timeline($user, $limit=10, $thumbnail=false, $cache_override=false) {

		if ($cache_override!=false) {
			$this->feed_cache = $cache_override;
		}

		// Get cached file
		if (file_exists($this->feed_cache) && strtotime('-'.$this->cache_time) < filemtime($this->feed_cache)  && $this->cache == true) {
			$return = file_get_contents($this->feed_cache);
		// Get new results
		} else {
	
			
			$token = '20512471-vpHT2n4WuRW2v7FDHdRpQkoY20dlxC4xjfyLi8kXH';
			$token_secret = 'C0AG3MHVUcV4Eh44KxUni5IZd7iDdn2e2gqTTSUZrtc';
			$consumer_key = 'DHQpFYAID9hahEZNKmB1cg';
			$consumer_secret = '5xv7pXtuva1PtT8Lpxed8ShvBRcFLb7JePcP4vjimao';
			
			$host = 'api.twitter.com';
			$method = 'GET';
			$path = '/1.1/statuses/user_timeline.json'; // api call path
			
			$query = array( // query parameters
			    'screen_name' => $user,
			    'count' => $limit
			);
			
			$oauth = array(
			    'oauth_consumer_key' => $consumer_key,
			    'oauth_token' => $token,
			    'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
			    'oauth_timestamp' => time(),
			    'oauth_signature_method' => 'HMAC-SHA1',
			    'oauth_version' => '1.0'
			);
			
			$oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
			$query = array_map("rawurlencode", $query);
			
			$arr = array_merge($oauth, $query); // combine the values THEN sort
			
			asort($arr); // secondary sort (value)
			ksort($arr); // primary sort (key)
			
			// http_build_query automatically encodes, but our parameters
			// are already encoded, and must be by this point, so we undo
			// the encoding step
			$querystring = urldecode(http_build_query($arr, '', '&'));
			
			$url = "https://$host$path";
			
			// mash everything together for the text to hash
			$base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);
			
			// same with the key
			$key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);
			
			// generate the hash
			$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));
			
			// this time we're using a normal GET query, and we're only encoding the query params
			// (without the oauth params)
			$url .= "?".http_build_query($query);
			
			$oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
			ksort($oauth); // probably not necessary, but twitter's demo does it
			
			// also not necessary, but twitter's demo does this too
			function add_quotes($str) { return '"'.$str.'"'; }
			$oauth = array_map("add_quotes", $oauth);
			
			// this is the full value of the Authorization line
			$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));
			
			// if you're doing post, you need to skip the GET building above
			// and instead supply query parameters to CURLOPT_POSTFIELDS
			$options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
			                  //CURLOPT_POSTFIELDS => $postfields,
			                  CURLOPT_HEADER => false,
			                  CURLOPT_URL => $url,
			                  CURLOPT_RETURNTRANSFER => true,
			                  CURLOPT_SSL_VERIFYPEER => false);
			
			// do our business
			$feed = curl_init();
			curl_setopt_array($feed, $options);
			$json = curl_exec($feed);
			curl_close($feed);
			
			$twitter = json_decode($json, true);


			$results = count($twitter);
		
			if ($results>$limit) {
				$max=$limit;
			} else {
				$max=$results;
			}
		
			$color[]='tweet_odd'; // styles for odd rows
			$color[]='tweet_even'; // styles for even rows
		
			$c=0;
	
			$return = '';

			$return = $return . '<ul class="twitter_holder">';
	
			for ($i=0; $i<$max; $i++) {
		
				$tweet_id = $twitter[$i]['id_str'];
				$user = $twitter[$i]['user']['screen_name'];
	
				if ($thumbnail=='thumbnail') {
					$thumb = '<img src="'.$twitter[$i]['user']['profile_image_url'].'" alt="Profile Image" class="tweet_img" />';
				} else {
					$thumb='';
				}
	
				$date = $this->get_time($twitter[$i]['created_at']);
				$tweet = $this->linker($twitter[$i]['text'], 'twitter_link');
	
//				$return = $return . '<li id="tweet_'.$tweet_id.'" class="'.$color[$c].'">';
				$return = $return . '<li>';
				$return = $return . $thumb.'<h2>HACS Charity</h2><h3>@HACSCharity</h3><p>'.$tweet.'</p>';
				$return = $return . '<span class="tweet_source"><h4>Posted <a href="http://twitter.com/#!/'.$user
//				$return = $return . '<span>Posted <a href="http://twitter.com/#!/'.$user
//				.'/status/'.$tweet_id.'">'.$date.'</a> from '.$twitter[$i]['source'].'</span>';
				.'/status/'.$tweet_id.'">'.$date.'</a></h4></span>';

				$return = $return . '</li>';
				

							
				$c++; if ($c==2) {$c=0;}
			}

	//		$return = $return . '<div class="twitter_date">Last updated '.date("M d @ g:i a").' pst</div>';
			$return = $return . '</ul>';

			$fh = fopen($this->feed_cache, 'w');
			fwrite($fh, $return);
			fclose($fh);

		}

		return $return;
	
	} // end display()



	function linker($link, $class) {
		$link = str_replace('http://www.', 'http://[w]', $link);
		$link = preg_replace('/(http:\/\/[^\s]+)/', "<a href=\"http://$1\">$1</a>", $link);
		$link = preg_replace('/(www\.[^\s]+)/', "<a href=\"http://$1\">$1</a>", $link);
		$link = preg_replace('/(^|\s)@(\w+)/', '\1<a class="$class" href="http://twitter.com/\2">@\2</a>', $link);
		$link = preg_replace('/(\()@(\w+)/', '\1<a class=$class" href="http://twitter.com/\2">@\2</a>', $link);
		$link = preg_replace('/(^|\s)#(\w+)/','\1<a class="$class" href="http://twitter.com/#!/search?q=%23\2">#\2</a>', $link);
		$link = str_replace('http://[w]', 'http://www.', $link);
		$link = str_replace('http://http://', 'http://', $link);
		return $link;
	} // end linker
	
	function get_time($twitter_time) {
	
		$time = strtotime($twitter_time);
		$date = time() - $time;
		$days = '';

		if ($date < 60) {
			$return = 'less than a minute ago.';
		} else if ($date < 120) {
			$return = 'about a minute ago.';
		} else if ($date < (45 * 60)) {
			$return = floor($date / 60) . ' minutes ago.';
		} else if ($date < (90 * 60)) {
			$return = 'about an hour ago.';
		} else if ($date < (24 * 60 * 60)) {
			$return = 'about ' . floor($date / 3600) . ' hours ago.';
		} else if ($date < (48 * 60 * 60)) {
			$return = '1 day ago.';
		} else {
			$return = floor($date / 86400) . ' days ago.';
			$days = floor($date / 86400);
		}
	
		if ($days>31) {
			$date = strtotime($twitter_time);
			$date = date( 'F d, Y', $date );
			$return = $date;
		}
	
		return $return;
	
	
	} // end get_time

}

$twitter = new twitter_plugin;

$twitter->timeline('mrkeithy', 5, true, false);   // don't forget to change the twitter handle

#2

I think this is a PHP question as much as a Twitter API question! Note that you probably don’t want to publish your API keys in sample code you post on the forums, you should keep those private.

Your code appears to work in terms of pulling 5 tweets for @mrkeithy. I suspect that the issue is the code uses a single file (feed_cache) to deposit the tweets, and if you try to call the timeline() function again, that file will be overwritten, as at line 156 the file is opened in w mode instead of a mode - (over)write instead of append.

So my advice would be either to pull a single line from the cache file to get the latest tweet to put in the footer; or use a different cache file for a second invocation of the code, and put the 3 tweets in one, and the one tweet in the other.


#3

Did you know? You can highiight your code samples using the & tags. Many languages are supported like …