Skip to content

Examples ​

The examples use Guzzle, but any HTTP client works. Every endpoint also has a cURL example in the CP.

Auth and call an API method ​

Authenticate once with the Auth API and use the session ID for the next requests.

php
$client = new \GuzzleHttp\Client(['base_uri' => 'https://your-domain.com/index.php/webservice/']);

// authenticate
$auth = json_decode((string) $client->post('auth', [
    'json' => [
        'data' => [
            'username' => 'my-username',
            'password' => 'my-password',
        ],
    ],
])->getBody(), true);

$session_id = $auth['data'][0]['session_id'];

// read an entry with the session ID
$response = $client->get('entry', [
    'headers' => ['X-Session-Id' => $session_id],
    'query' => ['data' => ['entry_id' => 1]],
]);

print_r(json_decode((string) $response->getBody(), true));

Create an entry ​

php
$client = new \GuzzleHttp\Client(['base_uri' => 'https://your-domain.com/index.php/webservice/']);

$response = $client->post('entry', [
    'headers' => ['Authorization' => 'Bearer my-shortkey'],
    'json' => [
        'data' => [
            'channel_name' => 'news',
            'title' => 'My new entry',
            'status' => 'open',
            // any other custom field can be added here
            'custom_field' => 'value',
        ],
    ],
]);

Read an entry ​

php
$response = $client->get('entry', [
    'headers' => ['Authorization' => 'Bearer my-shortkey'],
    'query' => ['data' => ['entry_id' => 39]],
]);

Update an entry ​

php
$response = $client->put('entry', [
    'headers' => [
        'X-Api-Key' => '83a4eac91b99ea0b3e3bb62e54d9c1b3',
        'X-Api-Secret' => '37e13a465cbd94b8e4cd4dbd260e2a22',
    ],
    'json' => [
        'data' => [
            'entry_id' => 39,
            'title' => 'new title',
            'custom_field' => 'Updated value',
        ],
    ],
]);

Delete an entry ​

php
$response = $client->delete('entry', [
    'headers' => ['Authorization' => 'Bearer my-shortkey'],
    'query' => ['data' => ['entry_id' => 39]],
]);

Without an HTTP client (cURL in PHP) ​

php
$ch = curl_init('https://your-domain.com/index.php/webservice/entry');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Authorization: Bearer my-shortkey',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'data' => [
        'entry_id' => 39,
        'title' => 'new title',
    ],
]));

$response = json_decode(curl_exec($ch), true);
curl_close($ch);