Issue tracker: https://phabricator.wikimedia.org/project/profile/1490/
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"
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( 'https://en.wikipedia.org/wiki/Berlin' );
You can easily log in and out:
$api->login( new \Mediawiki\Api\ApiUser( 'username', 'password' ) );
$api->logout();
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' ) );
try{
$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'] )