A basic Mediawiki api base lib

2.2.1 2016-08-03 18:24 UTC


Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version Download count

Issue tracker:

addwiki/mediawiki-api-base is a PHP HTTP client wrapped around guzzle that makes it easy to interest with a mediawiki installation.

  • Uses PSR-3 interfaces for logging
  • Handles Mediawiki login, sessions, cookies and tokens
  • Handles response errors by throwing catchable UsageExceptions
  • Retries failed requests where possible
  • Allows Async requests


Use composer to install the library and all its dependencies:

composer require "addwiki/mediawiki-api-base:~2.1"

Example Usage

You can get an api object by simply passing the api endpoint:

$api = \Mediawiki\Api\MediawikiApi::newFromApiEndpoint( 'http://localhost/w/api.php' );

You can event just pass a page:

$api = \Mediawiki\Api\MediawikiApi::newFromPage( '' );

You can easily log in and out:

$api->login( new \Mediawiki\Api\ApiUser( 'username', 'password' ) );

And make various requests:

use Mediawiki\Api\FluentRequest;
use Mediawiki\Api\SimpleRequest;

$api->postRequest( FluentRequest::factory()->setAction( 'purge' )->setParam( 'titles', 'FooBar' ) );

$queryResponse = $api->getRequest( FluentRequest::factory()->setAction( 'query' )->setParam( 'meta', 'siteinfo' ) );

    $api->postRequest( new SimpleRequest( 'FooBarBaz' ) );
catch ( UsageException $e ) {
    echo "Oh no the api returned an error!";

Including async requests:

use Mediawiki\Api\FluentRequest;

// Initiate each request but do not block
$requestPromises = array(
    'Page1' => $api->postRequestAsync( FluentRequest::factory()->setAction( 'purge' )->setParam( 'titles', 'Page1' ) ),
    'Page2' => $api->postRequestAsync( FluentRequest::factory()->setAction( 'purge' )->setParam( 'titles', 'Page2' ) ),
    'Page3' => $api->postRequestAsync( FluentRequest::factory()->setAction( 'purge' )->setParam( 'titles', 'Page3' ) ),

// Wait on all of the requests to complete.
$results = GuzzleHttp\Promise\unwrap( $requestPromises );

// You can access each result using the key provided to the unwrap function.
print_r( $results['Page1'], $results['Page2'], $results['Page3'] )