PHP + Prepr CMS

This package is a wrapper for the Prepr API.

Basics

The SDK on GitHub
Minimal PHP version: > 5.6.4
Requires GuzzleHttp 7.0.X, Murmurhash 2.0.X

Installation

You can install the SDK as a composer package.

composer require preprio/php-sdk

Making your first request

Let's start with getting all publications from your Prepr Environment.

<?php

use Preprio\Prepr;

$apiRequest = new Prepr('{ACCESS_TOKEN}');

$apiRequest
    ->path('publications')
    ->query([
        'fields' => 'items'
    ])
    ->get();

if($apiRequest->getStatusCode() == 200) {

    print_r($apiRequest->getResponse());
}

To get a single publication, pass the Id to the request.

<?php

use Preprio\Prepr;

$apiRequest = new Prepr('{ACCESS_TOKEN}');

$apiRequest
    ->path('publications/{id}', [
        'id' => '1236f0b1-b26d-4dde-b835-9e4e441a6d09'
    ])
    ->query([
        'fields' => 'items'
    ])
    ->get();

if($apiRequest->getStatusCode() == 200) {

    print_r($apiRequest->getResponse());
}

A/B testing with Optimize

To enable A/B testing you can pass a User ID to provide a consistent result. The A/B testing feature requires the use of the cached CDN API.

To switch off A/B testing, pass NULL to the UserId param.

$apiRequest = new Prepr('{ACCESS_TOKEN}', '{{YourCustomUserId}}');

or per request

$apiRequest
    ->path('publications/{id}',[
        'id' => 1
    ]),
    ->query([
        'fields' => 'example'
    ])
    ->userId(
        '{{YourCustomUserId}}'
    )
    ->get();

if($apiRequest->getStatusCode() == 200) {

    print_r($apiRequest->getResponse());
}

For more information check the Optimize documentation.

Using the CDN

To use Prepr in production we advise you to use the API CDN for a fast response. Add the API CDN Url to the init of the SDK.

<?php

use Preprio\Prepr;

$apiRequest = new Prepr('{ACCESS_TOKEN}', null, 'https://cdn.prepr.io/');

Auto paging results

To get all resources for an endpoint, you can use the auto paging feature.

$apiRequest
    ->path('publications')
    ->query([
        'limit' => 200 // optional
    ])
    ->autoPaging();

if($apiRequest->getStatusCode() == 200) {

    print_r($apiRequest->getResponse());
}

Override the AccessToken in a request

The authorization can also be set for one specific request ->url('url')->authorization('token').

Debug Errors

With $apiRequest->getRawResponse() you can get the raw response from the Prepr API.