Skip to content

Instantly share code, notes, and snippets.

@sajhd
Last active December 14, 2017 14:25
Show Gist options
  • Select an option

  • Save sajhd/f8cb0c5c21d21a9da29ce4507b229f1c to your computer and use it in GitHub Desktop.

Select an option

Save sajhd/f8cb0c5c21d21a9da29ce4507b229f1c to your computer and use it in GitHub Desktop.
Pull in tweet using API
<?php
// Grab the Authentication Library
include locate_template('parts/twitteroauth/autoload.php');
use Abraham\TwitterOAuth\TwitterOAuth;
// Store some secret keys
$connection_key = 'KEY-HERE';
$connection_secret = 'SECRET-HERE';
$access_token = 'TOKEN-HERE';
$access_secret = 'SECRET-HERE';
// Now connect to the Twitter API
$connection = new TwitterOAuth($connection_key, $connection_secret, $access_token, $access_secret);
// Verify everything
$check = $connection->get("account/verify_credentials");
// Are we verified?
if( $check->id ) $verify = true;
// How many tweets to pull in
$number_of_tweets = 3;
// Hide replies to the tweets
$hide_replies = true;
// Type of timeline to fetch
$timeline_type = 'statuses/user_timeline';
// Hide Retweets
$show_retweets = false;
// Check to see if we are verified else do the error messages below
if( $verify ) :
// Store the status response in a variable for later access
$status = $connection->get($timeline_type, ['count' => $number_of_tweets, 'exclude_replies' => $hide_replies, 'include_rts' => $show_retweets]);
//var_dump($status);
// We may have verified but do we have anything to show?
// If we do...
if( $status[0]->text ) :
// Start a counter
$i = 0;
// Now start a foreach loop to go over the status response
foreach ($status as $tweet) :
// UnComment this to see the tweet object
//var_dump($tweet);
?>
<p class="hp-news-item">
<a href="<?php echo $tweet->entities->urls[$i]->url; ?>">
<?php echo $tweet->text; ?>
<i class="fa fa-arrow-right"></i>
</a>
</p>
<?php $i++;
endforeach; // Endforeach of status loop
// We don't have anything to show
else: ?>
<p>Unfortunately, we are unable to fetch any tweets. <a href="https://twitteroauth.com/" target="_blank">See here for info.</a></p>
<?php foreach( $status->errors as $error) : ?>
<p>Error code: <?php echo $error->code; ?>. Error Message: <?php echo $error->message; ?></p>
<?php endforeach; ?>
<?php
endif;
// Otherwise we have received an error response to authentication. Loop over each error and print a code and message
else : ?>
<p>Unfortunately, we are unable to fetch any tweets. <a href="https://twitteroauth.com/" target="_blank">See here for info.</a></p>
<?php foreach( $check->errors as $error) : ?>
<p>Error code: <?php echo $error->code; ?>. Error Message: <?php echo $error->message; ?></p>
<?php endforeach; ?>
<?php
endif;
// Follow User
$user_id = $status[0]->user->id_str;
$follow = $connection->post('friendships/create', ['screen_name' => 'YOUR-TWITTER-HANDLE', 'follow' => true]);
// var_dump($follow);
?>
<a href="https://twitter.com/intent/follow/<?php echo $status[0]->user->id_str; ?>" class="text-link-green">Follow us on Twitter <i class="fa fa-arrow-right"></i></a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment