这里有一个想法:
您始终可以刮取插件的下载html页面:
http://wordpress.org/extend/plugins/some-plugin-slug/stats/
而今天的下载计数器并没有任何json/xml/rss源。
您感兴趣的html部分有以下表单:
<div id="history" class="left">
<h4>History</h4>
<table>
<tr>
<th scope="row">Today</th>
<td>7,390</td>
</tr>
<tr>
<th scope="row">Yesterday</th>
<td>12,262</td>
</tr>
<tr>
<th scope="row">Last Week</th>
<td>130,130</td>
</tr>
<tr class="last-child">
<th scope="row">All Time</th>
<td>13,639,901</td>
</tr>
</table>
</div>
您可以使用wp\\u remote\\u get()并缓存结果。
下面是一个获取数据的函数,它将plugin\\u slug(如akismet)作为输入:
function get_todays_downloads($plugin_slug){
$downloads_today="?";
$url="http://wordpress.org/extend/plugins/".$plugin_slug."/stats/";
// fetch the html page:
//
$response = wp_remote_get( $url );
if( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
//echo "Something went wrong:". $error_message;
// let\'s show "error" if we have problems fetching the page:
//
$downloads_today="error";
} else {
// get the body of the page:
//
$html=$response[\'body\'];
// let\'s strip out the newlines and tabs:
//
$html=str_replace("\\n","",$html);
$html=str_replace("\\t","",$html);
// let\'s find this type of html string:
// Today</th><td>12</td></tr>
//
preg_match(\'/Today<\\/th><td>([0-9,]+)<\\/td><\\/tr>/i\', $html, $matches);
// check if we got a match:
//
if(isset($matches[1])){
$downloads_today=$matches[1];
}
}
return $downloads_today;
}
您可以这样使用它:
// let\'s find out how many times the Akismet plugin has been downloaded today
//
echo "Downloads today: ".get_todays_downloads(\'akismet\');
它将为您提供如下结果:
Downloads today: 7,397
ps:请记住,页面的html将来可能会更改;-)