WordPress.ORGの「Plugin Directory」に登録されているプラグインのダウンロード数を取得する。
WP-Stats-Dashboardからコードを流用。
設定: プラグインの個別ページのURL。
ダウンロード数の取得: stat_count()を実行。
<?php /** Author: dligthart Origin: WP-Stats-Dashboard(WPSDStats.php, WPSDPluginStats.php) Update: redcocker 2012/2/13 **/ function stat_count() { // Setting $address = 'プラグインページのURL'; $xml = fetchDataRemote($address); $stat_count = get_stat_count($xml); return $stat_count; } function fetchDataRemote($url, $agent = 1) { if ('' == $url) return false; $xml = ''; if (function_exists('curl_init')) { $ch = curl_init(); $timeout = 2; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); switch ($agent) { default: case 1: curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13"); break; case 2: curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.8) Gecko/20100722 BTRS86393 Firefox/3.6.8 ( .NET CLR 3.5.30729; .NET4.0C)"); break; case 3: curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); break; } $xml = curl_exec($ch); curl_close($ch); } else { $xml = file_get_contents($url); } return $xml; } function get_stat_count($data) { preg_match("@<strong>Downloads: </strong>(.*?)<br />@", $data, $matches); $str = str_replace(',','',$matches[1]); settype($str, 'integer'); return number_format($str); } ?>
これだけのために余計なトラフィックを生むのが難点。APIがあれば・・・。