I am making a wep app using the Twitter API 1.1 and have successfully created functions that pull-in a specified users profile image, their actual name and their latest tweet. All using OAuth to authenticate the requests.
My question is this – All 3 functions created expect a twitter user name to be defined. At the moment I have to duplicate my code over and over for each twitter user name I put in. Surely there is a better way of doing it? As at the moment my code doesn’t look good at all. If anyone could help I would greatly appreciate it. I’m pretty new to PHP so am stuck on what to do.
Please see my code below:
The functions:
function getTweets($user, $tmhOAuth){
$usertweets = array();
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
'include_entities' => '1',
'include_rts' => '1',
'screen_name' => $user,
'count' => 1,
'exclude_replies' => 'true',
'contributor_details' => 'true'
));
if ($code == 200) {
$timeline = json_decode($tmhOAuth->response['response'], true);
foreach ($timeline as $tweet) :
$entified_tweet = tmhUtilities::entify_with_options($tweet);
$is_retweet = isset($tweet['retweeted_status']);
$diff = time() - strtotime($tweet['created_at']);
if ($diff < 60*60)
$created_at = floor($diff/60) . ' minutes ago';
elseif ($diff < 60*60*24)
$created_at = floor($diff/(60*60)) . ' hours ago';
else
$created_at = date('d M', strtotime($tweet['created_at']));
$permalink = str_replace(
array(
'%screen_name%',
'%id%',
'%created_at%'
),
array(
$tweet['user']['screen_name'],
$tweet['id_str'],
$created_at,
),
'<a href="https://twitter.com/%screen_name%/%id%">%created_at%</a>'
);
$tweet['created_at'] = $created_at;
$usertweets[] = $tweet;
endforeach;
} else {
tmhUtilities::pr($tmhOAuth->response);
}
return $usertweets;
}
function getImage($screen_name,$tmhOAuth,$size = ''){
$url = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/users/show'), array(
'screen_name' => $screen_name,
));
$results = json_decode($tmhOAuth->response['response'], true);
//Get the user's profile image
$profileImg = ($results['profile_image_url']);
return str_replace('_normal', $size, $profileImg);
}
function getName($screen_name,$tmhOAuth){
$url = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/users/show'), array(
'screen_name' => $screen_name,
));
$results = json_decode($tmhOAuth->response['response'], true);
//Get the user's name
$name = ($results['name']);
return $name;
}
How the functions are implemented:
$name2 = getName("JoeyEssex_",$tmhOAuth);
$user2 = getTweets("JoeyEssex_",$tmhOAuth);
$image2 = getImage("JoeyEssex_",$tmhOAuth);
$name3 = getName("piersmorgan",$tmhOAuth);
$user3 = getTweets("piersmorgan",$tmhOAuth);
$image3 = getImage("piersmorgan",$tmhOAuth);
$name4 = getName("BinkyFelstead",$tmhOAuth);
$user4 = getTweets("BinkyFelstead",$tmhOAuth);
$image4 = getImage("BinkyFelstead",$tmhOAuth);
//Display the profile images of specified users
echo '<img src="' . $image2 . '" width="240px" height="240px" />';
echo '<img src="' . $image3 . '" width="240px" height="240px" />';
echo '<img src="' . $image4 . '" width="240px" height="240px" />';
//Display user's name
echo '<p>Latest tweet from <b>' . $name1 . '</b>:<br />';
//Display their tweets
foreach($user1 as $tweet){
echo $tweet['text'] . '<br />';
echo "Sent: <b>" . $tweet['created_at'] . "</b></p>";
}
echo '<b>' . $name2 . '</b> ';
foreach($user2 as $tweet){
echo $tweet['text'];
echo " " . $tweet['created_at'] . "<br />";
}
echo '<b>' . $name3 . '</b> ';
foreach($user3 as $tweet){
echo $tweet['text'];
echo " " . $tweet['created_at'] . "<br />";
}
echo '<b>' . $name4 . '</b> ';
foreach($user4 as $tweet){
echo $tweet['text'];
echo " " . $tweet['created_at'] . "<br />";
}
You see the horrible repetition? There’s got to be a better way!
The only part that differs in the duplicated code is actually the name of the person tweeting. So create a list of all the tweeters names and iterate over it. Change the code inside the
foreachloop to reflect the new input and processing:Putting the repetition into a loop is a common way to express code-duplication in the meaning to do the same over and over again.
Additionally you can even start to wrap the functionality into a
Tweeterclass:This is quickly written, naturally you can even move the code from the existing
get...functions into the class methods, but I kept it step-by-step doing a quick first change without removing existing code.The usage then is fairly straight forward:
This will enable you to start profiting from object oriented programming.