Skip to content

Instantly share code, notes, and snippets.

@maucherOnline
Last active March 3, 2023 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maucherOnline/5a2126d37be4256c80956d4e8a057ede to your computer and use it in GitHub Desktop.
Save maucherOnline/5a2126d37be4256c80956d4e8a057ede to your computer and use it in GitHub Desktop.
Get revenues from freemius API
<?php
// Freemius PHP SDK needs to be installed
require_once dirname(__FILE__) . '/freemius/Freemius.php';
// Set credentials
define( 'FS__API_SCOPE', 'developer' );
define( 'FS__API_DEV_ID', 00000 );
define( 'FS__API_PUBLIC_KEY', 'pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
define( 'FS__API_SECRET_KEY', 'sk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
// Set plugin id
$plugin_id = 00000;
// Init SDK.
$api = new Freemius_Api(
FS__API_SCOPE,
FS__API_DEV_ID,
FS__API_PUBLIC_KEY,
FS__API_SECRET_KEY
);
// Get subscription data
$offset = 0;
$count = 50;
$revenues = [];
$do_loop = true;
while ( $do_loop ) {
$data = $api->Api("/plugins/{$plugin_id}/subscriptions.json?count=".$count."&offset=".$offset);
$countResults = count($data->subscriptions);
if ( $countResults <= 0) $do_loop = false;
foreach ( $data->subscriptions as $row ) {
$revenues[] = $row;
}
$offset += $count;
}
// Set file data and header
$filename = date("Y-m-d").".csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=' . $filename);
header("Content-Transfer-Encoding: UTF-8");
$f = fopen('php://output', 'a');
// Transform stdClass into array
$rev_arr = json_decode(
json_encode(
$revenues
), true
);
// Get array keys as header columns
$arr_first = json_decode(
json_encode(
reset($rev_arr)
), true
);
// Set array keys as header columns
fputcsv($f,
array_keys(
$arr_first
),
'|'
);
// Write data into CSV
foreach ( $rev_arr as $row ) {
$arr = json_decode( json_encode( $row ), true);
fputcsv( $f, $arr, '|' );
}
// Close
fclose( $f );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment