Skip to content

superdesk/contentapi-sdk-php

Repository files navigation

Superdesk Content API PHP SDK

Build Status Scrutinizer Code Quality Code Climate SensioLabsInsight

This is an SDK written in PHP for the Superdesk Content API.

For more information about the Superdesk Content API please read the documentation.

Installation

Requirements

  • PHP >= 5.3

When using the CurlApiClient and CurlClient classes make sure you've installed the following PHP extensions:

Composer

  • Require the SDK as a composer dependency
$ php composer.phar require superdesk/contentapi-sdk-php

Manual

  • Download a copy of the SDK
  • Add the classes to your autoloader

Customization

You can use your own client classes instead of CurlClient and CurlApiClient files. This is for example useful if your framework or project already has it's own http client, you can easily incorporate that without having multiple depencencies. All you need to do is to implement the ClientInterface and ClientApiInterface. For the ClientApi class there also a useful abstract class AbstractApiClient which contains some sane defaults.

Examples

This example uses the CurlClient and CurlApiClient files.

You can run the example.php via the cli with the command:

$ php sample/default-client/example.php

Make sure you have the extenions cURL and Multibyte String enabled and you've installed the vendors.

$ php composer.phar install --no-dev

Samples

Authentication

OAuth username and password authentication. The CurlApiClient will automatically try to retrieve new access token if the previous token has been invalited.

<?php

use Superdesk\ContentApiSdk\ContentApiSdk;
use Superdesk\ContentApiSdk\Client\CurlClient;
use Superdesk\ContentApiSdk\Client\CurlApiClient;
use Superdesk\ContentApiSdk\Api\Authentication\OAuthPasswordAuthentication;

$genericClient = new CurlClient();
$authentication = new OAuthPasswordAuthentication($genericClient);
$authentication
    ->setClientId(API_CLIENT_ID)
    ->setUsername(API_USERNAME)
    ->setPassword(API_PASSWORD);
$apiClient = new CurlApiClient($genericClient, $authentication);
$contentApi = new ContentApiSdk($apiClient, API_HOST, API_PORT, API_PROTOCOL);

?>

Pagination and traversing through results

We use Pagerfanta as a pagination library. All results returned from the methods getItems(...) and getPackages(...) are actually Pagerfanta instances. That should make it easy enough to traverse through your API results and also built in your own pagination.

<?php

use Superdesk\ContentApiSdk\ContentApiSdk;

$contentApi = new ContentApiSdk(...);

$maxPerPage = 1;
$startPage = 1;

$items = $contentApi->getItems(array(), $startPage, $maxPerPage);

$items->getNbResults(); // Total items
$items->getMaxPerPage(); // Max results per age == $maxPerPage
$items->getNbPages(); // Total pages

?>