Last active
December 18, 2015 14:19
-
-
Save gerbenvandijk/5796624 to your computer and use it in GitHub Desktop.
Get tweets (using the Twitter API 1.1) and cache them using the Wordpress Transient API. You need twitter-api-php (http://github.com/j7mbo/twitter-api-php).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function ShowTweets(){ | |
// Setting the name of our transient | |
$transName = 'twitter'; | |
// Time that the transient expires (in minutes) | |
$cacheTime = 10; | |
// If the transient doesn't exist, or when the transient has expired | |
if(false === ($data = get_transient($transName))){ | |
// Require the Twitter-API-PHP : Simple PHP wrapper for the v1.1 API (http://github.com/j7mbo/twitter-api-php) | |
require_once('TwitterAPIExchange.php'); | |
// Set access tokens here - see: https://dev.twitter.com/apps/ | |
$settings = array( | |
'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN", | |
'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET", | |
'consumer_key' => "YOUR_CONSUMER_KEY", | |
'consumer_secret' => "YOUR_CONSUMER_SECRET" | |
); | |
// Set the url for the Twitter search | |
$url = 'https://api.twitter.com/1.1/search/tweets.json'; | |
// Pass all the search parameters (https://dev.twitter.com/docs/api/1.1/get/search/tweets) | |
$getfield = '?q=%23dv2013'; | |
$requestMethod = 'GET'; | |
// Now we get the tweets from Twitter-API-PHP | |
$twitter = new TwitterAPIExchange($settings); | |
// We get the JSON data from the requested tweets | |
$data = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(); | |
// We decode the JSON to get a PHP array | |
$data = json_decode($data, TRUE); | |
// We store the data in a transient | |
set_transient($transName, $data, 60 * $cacheTime); | |
} | |
// We get the tweets from the stored transient | |
$data = get_transient($transName); | |
// We get the statuses array (containing all the tweets from the search we are performing) | |
$data = $data['statuses']; | |
// Check we have any tweets | |
if(is_array($data)) { | |
// If we have tweets, we print the container of the tweets | |
print('<div class="slides_container">'); | |
// Get the first 5 tweets | |
array_splice($data, 5); | |
// For each tweet | |
foreach($data as $tweet) { | |
// The user data is in a nested array, so here we define it | |
$user = $tweet['user']; | |
// Print the tweet (change the markup to your liking) | |
print('<div class="slide"><p><a href="https://twitter.com/' .$user['screen_name']. '" target="_blank">@' .$user['name']. '</a> ' . HandleLinks($tweet['text']) . '</p></div>'); | |
} | |
// If we have tweets, we print the closing tag of the container of the tweets | |
print('</div>'); | |
} | |
} | |
// Function to create links in the tweet's text | |
function HandleLinks($v) { | |
// Creating links for URL's | |
$v = preg_replace('@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@','<a href="$1" target="_blank">$1</a>', $v); | |
// Creating links for usernames | |
$v = preg_replace('/@(\w+)/','<a href="http://twitter.com/$1" target="_blank">@$1</a>',$v); | |
// Creating links for hashtags | |
$v = preg_replace('/\s+#(\w+)/',' <a href="https://twitter.com/search?q=%23$1" target="_blank">#$1</a>', $v); | |
return trim($v); | |
} | |
// Usage: | |
ShowTweets(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm a little lost ...
Where should I enter the username?