コンテンツへスキップ
2011/07/03 / Javelina

PHPでURLに対するfacebookのいいね!数とtwitterのtweet数を取得する

メモ。
特定のURL(ページ)に対するfacebookのいいね!数と、
twitterでのtweet数(つぶやかれた回数)をPHPで取得する方法。
 

<?php
$source_url = urlencode('取得したいページのURL');

$get_facebook = 'http://api.facebook.com/restserver.php?method=links.getStats&urls=' . $source_url;
$xml = file_get_contents($get_facebook);
$xml = simplexml_load_string($xml);
$likes = $xml->link_stat->like_count;//いいね!数。※シェア数ならshare_countで

$get_twitter = 'http://urls.api.twitter.com/1/urls/count.json?url=' . $source_url;
$json = file_get_contents($get_twitter);
$json = json_decode($json);
$tweets = $json->{'count'};//ツイート数
?>

 
これで多分いけるはず~
 

おまけではてなブックマーク数の取得

<?php
$get_hatebu = 'http://api.b.st-hatena.com/entry.count?url=' . $source_url;
$hatebu = file_get_contents($get_hatebu);//はてなブックマーク数
?>

 

コメントを残す