Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress plugin API sample-use
<?php
/*
Retrieve items from the WordPress.org plugin API
Original source: http://pastebin.com/7Ji8rD2P
Docs v1.0: http://dd32.id.au/projects/wordpressorg-plugin-information-api-docs/
Usage:
http://{host}/wordpress-plugin-api.php?page_start=9&per=20&pages=10&info=1
GET['page_start'] => Page-numer to start retreiving data on
GET['per'] => Plugins to show per-page
GET['pages'] => Number of pages to retrieve
GET['info'] => Show total pagination data: Any # other than 0 => true; Empty or 0 => false
*/
function get_api_data( $per_page, $page )
{
$fields = array(
'downloaded' => true,
'author' => false,
'author_profile' => false,
'contributors' => false,
'requires' => true,
'tested' => false,
'compatibility' => false,
'homepage' => false,
'description' => false,
'last_updated' => true,
'added' => true
);
$body = (object) array('browse' => 'new', 'page' => $page, 'per_page' => $per_page, 'fields' => $fields);
$post_data = array('action' => 'query_plugins', 'request' => serialize($body));
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, 'http://api.wordpress.org/plugins/info/1.0/');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
return unserialize($return);
}
// Manual pagination using GET params
$item_count = ( isset($_GET['per']) && is_numeric($_GET['per']) ) ? $_GET['per'] : 50;
$page_start = ( isset($_GET['page_start']) && is_numeric($_GET['page_start']) ) ? $_GET['page_start'] : 1;
$page_count = ( isset($_GET['pages']) && is_numeric($_GET['pages']) ) ? $_GET['pages'] : 1;
$display_meta = ( isset($_GET['info']) && is_numeric($_GET['info']) ) ? (bool)$_GET['info'] : false;
if ( $display_meta ) {
$plugin_api_raw = get_api_data(1, 1);
//var_dump( $plugin_api_raw -> info );
$number_plugins = $plugin_api_raw -> info;
echo '<h1>Plugins with available data:&nbsp;' . $number_plugins['results'] . '</h1><hr />';
}
// Write returned WP-plugin data to screen
for ( $i = 0; $i < $page_count; $i++ ) {
$current_page = $page_start + $i;
$plugin_api_raw = get_api_data( $item_count, $current_page );
$plugin_data_list = $plugin_api_raw -> plugins;
foreach( $plugin_data_list as $plugin_data_line ) {
// Output per-plugin info
foreach ( $plugin_data_line as $k => $v ) {
if ( $k == 'name' ) echo '<h2>' . $v . '</h2><p>';
else echo '<strong>' . ucfirst($k) . ':</strong>&nbsp;' . $v . '<br />';
}
echo '</p>';
}
}
@mbijon

This comment has been minimized.

Copy link
Owner Author

@mbijon mbijon commented Nov 2, 2011

After using this on Nov 1, 2011, the array of $fields does not appear to have any affect on what data is returned. Have bandwidth ready, all data appears to be returned regardless of $fields true|false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment