Skip to content

Instantly share code, notes, and snippets.

@tszym
Created July 8, 2014 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tszym/cc29db2acfc4d176b0ea to your computer and use it in GitHub Desktop.
Save tszym/cc29db2acfc4d176b0ea to your computer and use it in GitHub Desktop.
Twitter API - List my tweets
<?php
/**
* @file
* User has successfully authenticated with Twitter. Access tokens saved to session and DB.
*/
/* Load required lib files. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
/* If access tokens are not available redirect to connect page. */
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
header('Location: ./clearsessions.php');
}
/* Get user access tokens out of the session. */
$access_token = $_SESSION['access_token'];
/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
/* If method is set change API call made. Test is called by default. */
//$content = $connection->get('account/verify_credentials');
/* The calls */
$content = $connection->get('statuses/user_timeline');
/* Include HTML to display on the page
* This is where we use the response
*/
include('template-list-my-tweets.php');
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Twitter - List my tweets</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
img {border-width: 0}
* {font-family:'Lucida Grande', sans-serif;}
</style>
</head>
<body>
<div>
<h2>Twitter - List my tweets</h2>
<p>This page is a basic showcase of how to retrieve my list of tweets and retweets. If you are having issues try <a href='./clearsessions.php'>clearing your session</a>.</p>
<p>
Links about the lib author:
<a href='http://github.com/abraham/twitteroauth'>Source Code</a> &amp;
<a href='http://wiki.github.com/abraham/twitteroauth/documentation'>Documentation</a>
Contact @<a href='http://twitter.com/abraham'>abraham</a>
</p>
<hr>
<h2>My tweets</h2>
<?php foreach ($content as $tweet) {
echo '<h4>A tweet!</h4><br>';
echo '<p>', $tweet->text, '</p><br>';
} ?>
<hr>
<?php if (isset($menu)) {
echo $menu;
} ?>
</div>
<?php if (isset($status_text)) {
echo '<h3>'.$status_text.'</h3>';
} ?>
<p>
<pre>
<?php print_r($content); ?>
</pre>
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment