2
\$\begingroup\$

How can I script this in a better way?

// Tweets

$twitter_username_string = "aasampat OR ashwinsampat OR somemore";
$twitter_number_of_tweets = 1;
$twitter_hashtags = "ashwinsampat+OR+franq2+OR+franq2.net";

// Instantiate a Twitter object which will scan Twitter depending on the parameters
$twitter = new Twitter($twitter_username_string, $twitter_number_of_tweets, $twitter_hashtags);
$tweets = $twitter->getTweets();

foreach ( $tweets as $tweet )
{
    $tweetoutput = "<p>".$tweet->title." <em>Tweeted by <a href=".$tweet->permalink." target=_blank title=@".$tweet->user.">".$tweet->author."</em></a></p>";
}
$twitter_username_string = "aasampat OR ashwinsampat OR somemore";
$twitter_number_of_tweets = 2;
$twitter_hashtags = "ashwinsampat+OR+franq2+OR+franq2.net";

// Instantiate a Twitter object which will scan Twitter depending on the parameters
$twitter = new Twitter($twitter_username_string, $twitter_number_of_tweets, $twitter_hashtags);
$tweets = $twitter->getTweets();

foreach ( $tweets as $tweet )
{
    $tweetoutput_secondline = "<p>".$tweet->title." <em>Tweeted by <a href=".$tweet->permalink." target=_blank title=@".$tweet->user.">".$tweet->author."</em></a></p>";
}

$myFile = "export_file_twitter_output";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $tweetoutput;
fwrite($fh, $stringData);
fclose($fh);

$filename = 'export_file_twitter_output';
$somecontent = "$tweetoutput_secondline";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
\$\endgroup\$
1
\$\begingroup\$

There is a lot of duplication here. The code

  1. downloads one tweet, then stores it in the $tweetoutput variable,
  2. downloads two tweets, then stores the second in the $tweetoutput_secondline variable,
  3. opens a file called export_file_twitter_output, then writes the first tweet into it (and closes the file),
  4. opens the same export_file_twitter_output file and writes the second tweet into it (and closes the file).

I'd download simultaneously the two tweets ($twitter_number_of_tweets = 2), then store both in the same variable:

$tweetoutput = "";
foreach ( $tweets as $tweet )
{
    $tweetoutput .= "<p>" . $tweet->title . " <em>Tweeted by <a href=" .
        $tweet->permalink .
        " target=_blank title=@" . $tweet->user . ">" .
        $tweet->author . "</em></a></p>\n";
}

(Note the .=.)

After that I'd write the contents of $tweetoutput to the file in one step. This would remove a lot of code duplication and it would work with only one network connection instead of two.

I prefer the second file-writing code, it has error handling on fwrite and it checks that the file is writable before opening.

I'd also change the string concatenation to sprintf inside the foreach loop. It would result more readable code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.