Clone wiki

restclient-php / Home

PHP library restclient-php


  • Full-featured that means the client supports the whole functionality of the API.
  • contains a model such that it is convenient for you to work with the data.
  • supports Basic Auth as well as OAuth authentication methods.
  • easy to integrate in your PHP App, since we have used composer, a powerful tool in order to integrate and autoload requirements of 3rd party software.
  • outputs a fetched set of publications as a CSL-rendered publication list in your favorite citation style.

Basic Usage

Installation via Composer

Install composer to your project root folder:

$ curl -sS | php

Run composer and initialize your project.

$ php composer.phar init

Follow the prompt and define a package name, description, and author. When you'll be asked for dependencies type academicpuma/restclient-php and choose the latest version. After finishing the initalization composer creates a composer.json configuration file in the project root folder.

In order to fetch the dependencies and to get an autoload file run the following line in your terminal:

$ php composer.phar update

Composer creates a vendor folder including the autoload.php file and all the depended packages.

Now you can start developing!

Create an Accessor

The Accessor object is required to authenticate your App on the REST API of PUMA/BibSonomy. You can choose between two authentication methods, Basic Auth or OAuth. For Basic Auth this would be:

require 'path/to/vendor/autoload.php';

use AcademicPuma\RestClient\Accessor\BasicAuthAccessor;
$accessor = new BasicAuthAccessor('', 'your-username', 'your-apikey');

Using an OAuth Accessor is more complicated. An detailed description you can find here.

Use the RESTClient

The next step is getting an instance of the REST Client:

use AcademicPuma\RestClient\RESTClient;
use AcademicPuma\RestClient\Config;

$restClient = new RESTClient($accessor);

That's all you have to do for initialization! Now you can perform API requests:

//choose Resource type and tags
$restClient->getPosts(Config\Resourcetype::BIBTEX, Config\Grouping::USER, 'username', ['tag1', 'tag2']);

You have five different options to get the results: XML, CSL (JSON), Model (PHP Objects), Bibliography (CSL-rendered publication list), file:

//output xml
echo $restClient->xml();

//output CSL
echo json_encode($restClient->csl());

//use the model
$posts = $restClient->model();
foreach($posts as $post) {
    echo $post->getResource()->getTitle()."<br />\n";

//print a bibliography in your preferred style and language
echo $restClient->bibliography('apa', 'en-US');

If your request aims to fetch a document in order to provide it as a download you have to do the following steps:

$restClient->getDocumentFile($userName, $resourceHash, $fileName, Config\DocumentType::FILE);
header("Content-Disposition: attachment; filename=" . $fileName);
print $restClient->file()->getContents();

Advanced Usage