WP.org API: Accessing plugin downloads “Today” value?

Late answer

A mini plugin as local API

This plugin gives you – after you filled in the slug of your repository – the downloads stats as array. The keys are the dates, the values the downloads.

<?php
/** Plugin Name: (#84254) Plugin stats API */
function wpse84254_get_download_stats()
{
    $response = wp_remote_request(
        add_query_arg(
             'slug'
            ,'YOUR-REPO-PLUGIN-SLUG'
            ,'http://wordpress.org/extend/stats/plugin-xml.php'
        )
        ,array( 'sslverify' => false )
    );
    // Check if response is valid
    if ( is_wp_error( $response ) )
        return $response->get_error_message();
    if (
        empty( $response )
        OR 200 !== wp_remote_retrieve_response_code( $response )
        OR 'OK' !== wp_remote_retrieve_response_message( $response )
    )
        return _e( 'No Stats available', 'pluginstats_textdomain' );

    $response  = wp_remote_retrieve_body( $response );
    $response  = (array) simplexml_load_string( $response )->children()->chart_data;
    $response  = (array) $response['row'];
    $dates     = (array) array_shift( $response );
    $dates     = $dates['string'];
    // Get rid of unnecessary prepended empty object
    array_shift( $dates );
    $downloads = (array) array_pop( $response )->number;
    if ( count( $dates ) !== count( $downloads ) )
        return;

    $result = array_combine(
         $dates
        ,$downloads
    );
    return array_map(
         'absint'
        ,$result
    );
}

Usage

To get only the last day + the download number:

$data = array_unshift( wpse84254_get_download_stats );
echo key( $data ).' had '.$data.' Downloads';

Leave a Comment