<?php class SocialCounter { public static function getHatebu($url) { $count = 0; $url = 'http://b.hatena.ne.jp/bc/' . $url; $fp = fopen($url, 'r'); $meta_data = stream_get_meta_data($fp); foreach ($meta_data['wrapper_data'] as $header) { if (preg_match("/Location:.*\/([0-9]+)\.gif/", $header, $matches)) { $count = $matches[1] + 0; } } return $count; } public static function getTweet($url) { $count = 0; $url = 'http://urls.api.twitter.com/1/urls/count.json?url=' . $url; $res = file_get_contents($url); $j = json_decode($res); if (!is_null($j->count) && preg_match('/^[0-9]+$/', $j->count)) { $count = $j->count + 0; } return $count; } public static function getFbLike($url) { $count = 0; $url = 'http://api.facebook.com/restserver.php?method=links.getStats&urls=' . $url; $res = file_get_contents($url); if (strlen($res) > strlen("link_stat/total_count")*2) { $xml = simplexml_load_string($res); if (!is_null($xml->link_stat) && !is_null($xml->link_stat->total_count)) { $count = $xml->link_stat->total_count->__toString() + 0; } } return $count; } /** * @deprecated 取得方法不明 */ public static function getGplus1($url) {} }
使い方
<?php $url = "http://d.hatena.ne.jp/tottokug/20120105/1325753189"; $hatebu = SocialCounter::getHatebu($url); var_dump($hatebu); $tweet = SocialCounter::getTweet($url); var_dump($tweet); $like = SocialCounter::getFbLike($url); var_dump($like);