I wrote / modified two scripts to count Facebook and Twitter reactions. Anything I can do to improve these?
Count Facebook Reactions
class FacebookReactions
{
public $url;
function __construct($url)
{
$query = 'SELECT like_count, total_count FROM link_stat WHERE url="'.$url.'"';
$apifql="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($query);
$json=file_get_contents($apifql);
$fb_obj=json_decode($json);
$fb_array=object_2_array($fb_obj);
$this->total = $fb_array[0]['total_count'];
$this->like = $fb_array[0]['like_count'];
}
}
Count Twitter Reactions
class TwitterReactions
{
public $url = '';
public $output = array();
public $count = 0;
public $total = 100;
function __construct($url,$total)
{
$this->url = $url;
$query = 'http://search.twitter.com/search.json?q='.$url.'&result_type=mixed&rpp='.$total;
$reactions=file_get_contents($query);
$reactions_array=json_decode($reactions, TRUE);
foreach($reactions_array as $key => $results)
{
if(is_array($results)){
foreach($results as $key => $result){
$this->output[$key]['user'] = $result['from_user'];
$this->output[$key]['image'] = $result['profile_image_url'];
$this->output[$key]['message'] = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $result['text']);
$this->output[$key]['date'] = date('m.d.y',strtotime($result['created_at']));
}
}
}
$this->count = count($this->output);
}
}