Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:38:54+00:00 2026-06-15T15:38:54+00:00

I am making a wep app using the Twitter API 1.1 and have successfully

  • 0

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>&nbsp;';
foreach($user2 as $tweet){
echo $tweet['text'];
echo "&nbsp;" . $tweet['created_at'] . "<br />";
}

echo '<b>' . $name3 . '</b>&nbsp;';
foreach($user3 as $tweet){
echo $tweet['text'];
echo "&nbsp;" . $tweet['created_at'] . "<br />";
}

echo '<b>' . $name4 . '</b>&nbsp;';
foreach($user4 as $tweet){
echo $tweet['text'];
echo "&nbsp;" . $tweet['created_at'] . "<br />";
}

You see the horrible repetition? There’s got to be a better way!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:38:56+00:00Added an answer on June 15, 2026 at 3:38 pm

    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 foreach loop to reflect the new input and processing:

    $tweeters = array('JoeyEssex_', 'piersmorgan', 'BinkyFelstead');
    
    foreach ($tweeters as $tweeter)
    {
        $tweeter_name   = getName($tweeter, $tmhOAuth);
        $tweeter_tweets = getTweets($tweeter, $tmhOAuth);
        $tweeter_image  = getImage($tweeter, $tmhOAuth);    
    
        // Display the profile images of user
        echo '<img src="' . $tweeter_image . '" width="240px" height="240px" />';
    
        // Display user's name
        echo '<p>Latest tweet from <b>' . tweeter_name . '</b>:<br />';
    
        // Display user's tweets
        foreach ($tweeter_tweets as $tweet)
        {
            echo $tweet['text'] . '<br />';
            echo "Sent: <b>" . $tweet['created_at'] . "</b></p>";
        }
    }
    

    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 Tweeter class:

    class Tweeter {
    
        private $name;
        private $tmhOAuth;
    
        public function __construct($name, $tmhOAuth) {
            $this->name     = $name;
            $this->tmhOAuth = $tmhOAuth;
        }
    
        public function getName() {
            return getName($this->name, $this->tmhOAuth);
        }
    
        public function getTweets() {
            return getTweets($this->name, $this->tmhOAuth);
        }
    
        public function getImage() {
            return getImage($this->name, $this->tmhOAuth);
        }
    }
    

    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:

    $tweeters = array('JoeyEssex_', 'piersmorgan', 'BinkyFelstead');
    
    // Create all Tweeter objects
    foreach ($tweeters as $i => $tweeter)
    {
        $tweeters[$i] = new Tweeter($tweeter, $tmhOAuth);
    }
    
    // Display all Tweeters
    
    foreach ($tweeters as $tweeter)
    {
        echo '<img src="' . $tweeter->getImage() . '" width="240px" height="240px" />';
    
        echo '<p>Latest tweet from <b>' . $tweeter->getName() . '</b>:<br />';
    
        foreach ($tweeter->getTweets() as $tweet)
        {
            echo $tweet['text'] . '<br />';
            echo "Sent: <b>" . $tweet['created_at'] . "</b></p>";
        }
    }
    

    This will enable you to start profiting from object oriented programming.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Making a painting app using HTML5 and Canvas. I think I want to have
making a multi-language site with codeginiter. I have created two folders. One for french
Making sense out of an .MSI verbose trace. I created the .MSI using VisualStudio
Making game of life I need to a have a grid that is 30x20
Making a social media app. I have a main viewFlipper with several views therein.
I making a web app in Flex using global coordinates I get the coordinates
Making a ship game because I am incredibly original.. With that aside, I have
Making a word document of our network set-up. We have about 7 servers and
Making an adobe flex ui in which data that is calculated must use proprietary
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.