Skip to content

Instantly share code, notes, and snippets.

@lswilson
Created September 13, 2013 03:13
Show Gist options
  • Save lswilson/6546425 to your computer and use it in GitHub Desktop.
Save lswilson/6546425 to your computer and use it in GitHub Desktop.
Auto embed latest tweet from post author (based on Twitter ID field in author profile)
<?php
$curauth = get_userdata($post->post_author); //get data for current post author
$twitter_id = $curauth->twitter;
$api = array(
'consumer_key' => 'nbRYD9VZGOk8VDiqvuVg',
'consumer_secret' => 'MMtHXv9ErHjLEOcNJLjNWfC3WRVy1Qe3qHE4vl4Uo',
'oauth_access_token' => '1330201-jMrtoilTOGUCneecIV5yGYD6mUh8tmxbf5OWEUM',
'oauth_access_token_secret' => 'wzW875Vsj2xlGQivqvW1h35Zq7GkT6VqLVtBn0gJqY',
);
$tweets = ktp_get_tweets( $twitter_id, 1, $api );
foreach( $tweets as $chirp ) {
$tweet = preg_replace("/((http)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $chirp->text );
$tweet = preg_replace("/[@]+([A-Za-z0-9-_]+)/", "<a href=\"http://twitter.com/\\1\" target=\"_blank\">\\0</a>", $tweet );
$tweet = preg_replace("/[#]+([A-Za-z0-9-_]+)/", "<a href=\"http://twitter.com/search?q=%23\\1\" target=\"_blank\">\\0</a>", $tweet );
echo '<blockquote width="600" align="center" class="twitter-tweet"><a href="https://twitter.com/' .$twitter_id. '/status/' .$chirp->id_str. '</blockquote>';
echo '<script src="//platform.twitter.com/widgets.js" charset="utf-8"></script>';
}
function ktp_get_tweets( $user = 'twitter', $count = 1, $api ) {
// Set the URL and our tokens/keys
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
//Change me below, to your own tokens
$consumer_key = $api['consumer_key'];
$consumer_secret = $api['consumer_secret'];
$oauth_access_token = $api['oauth_access_token'];
$oauth_access_token_secret = $api['oauth_access_token_secret'];
// Set our variables
$oauth = array(
'screen_name' => $user,
'count' => $count,
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
// Put everything together
$base_info = ktp_tweet_build_base( $url, 'GET', $oauth );
$composite_key = rawurlencode( $consumer_secret ) . '&' . rawurlencode( $oauth_access_token_secret );
$oauth['oauth_signature'] = base64_encode( hash_hmac('sha1', $base_info, $composite_key, true ) );
// Make Requests
$options = array(
CURLOPT_HTTPHEADER => array( ktp_tweet_authorize_header( $oauth ), 'Expect:' ),
CURLOPT_HEADER => false,
CURLOPT_URL => $url . '?screen_name=' . $user . '&count=' . $count,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
);
// Build our feed
$feed = curl_init();
curl_setopt_array( $feed, $options );
$json = curl_exec( $feed );
curl_close( $feed );
$tweets = json_decode( $json );
return $tweets;
}
function ktp_tweet_build_base( $baseURI, $method, $params ) {
$r = array();
ksort( $params );
foreach( $params as $key=>$value ){
$r[] = "$key=" . rawurlencode( $value );
}
return $method."&" . rawurlencode( $baseURI ) . '&' . rawurlencode( implode( '&', $r ) );
}
function ktp_tweet_authorize_header( $oauth ) {
$r = 'Authorization: OAuth ';
$values = array();
foreach( $oauth as $key=>$value )
$values[] = "$key=\"" . rawurlencode( $value ) . "\"";
$r .= implode( ', ', $values );
return $r;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment