Jump to content

Recommended Posts

I'm a big fan of Big Brother UK and as a fun project I have built a Twitter hash tag counter to count the battle between the contestants this year. Using the API from Topsy.com I have built a counter that shows the amount of tweets daily, monthly and hourly. However, I wondered if it was possible for me to calculate the amount of tweets say between the time the page was loaded and the time a user clicks stop so I can see how the numbers have change during the show. Would it be possible to use the setTimeOut() function or is there a better way of doing this?

 

heres my code:

 

<?php
error_reporting(0);
$jsonurl     = "http://otter.topsy.com/searchcount.json?q=%23teamrylan&type=tweet&apikey=FJWCK5YSZIBEUZX4HYJQAAAAABLTPFQ3DVIQAAAAAAAFQGYA";
$json        = file_get_contents($jsonurl, 0, null, null);
$json_output = json_decode($json, true);
$json_output = array_values($json_output);
foreach ($json_output as $key => $val) {
   $hourly  = number_format($val["h"]);
   $daily   = number_format($val["d"]);
   $weekly  = number_format($val["w"]);
   $monthly = number_format($val["m"]);
   $alltime = number_format($val["a"]);
}


echo '<strong>How many times have people tweeted #TeamRylan</strong><br /><br />';
echo 'In the last Hour: ' . $hourly . '<br />';
echo 'In the last Day: ' . $daily . '<br />';
echo 'In the last Week: ' . $weekly . '<br />';
echo 'In the last Month: ' . $monthly . '<br />';
echo 'Ever: ' . $alltime . '<br />';

?>

Link to comment
https://forums.phpfreaks.com/topic/273232-twitter-hashtag-counter/
Share on other sites

Using cURL over file_get_contents will be about 3-5times faster than file_get_contents, so if you have the option to use cURL, I would opt for that for a bit more efficiency.

 

Also, you can display the text without echo's using <?php echo $weekly; ?> such as:

 

?>
<strong>How many times have people tweeted #TeamRylan</strong><br /><br />
In the last Hour: <?php echo $hourly; ?><br />
In the last Day: <?php echo $daily; ?><br />
In the last Week: <?php echo $weekly; ?><br />
In the last Month: <?php echo $monthly; ?><br />
Ever: <?php echo $alltime; ?> <br />

 

Granted the display part is more or less preference, but to me that would be easier to edit not having to worry about quotes etc. 

 

Finally, you do not need a foreach, instead just do this:

 

<?php
error_reporting(0);
$jsonurl         = "http://otter.topsy.com/searchcount.json?q=%23teamrylan&type=tweet&apikey=FJWCK5YSZIBEUZX4HYJQAAAAABLTPFQ3DVIQAAAAAAAFQGYA";
$json           = file_get_contents($jsonurl, 0, null, null);
$json_output = json_decode($json, true);

$hourly  = number_format($json_output['response']["h"]);
$daily   = number_format($json_output['response']["d"]);
$weekly  = number_format($json_output['response']["w"]);
$monthly = number_format($json_output['response']["m"]);
$alltime = number_format($json_output['response']["a"]);

 

None of this stuff is really necessary, just some items that will help efficiency a little bit, ie not having to loop etc. 

 

Best of luck.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.