PHP code example of sitepark / oparl-client

1. Go to this page and download the library: Download sitepark/oparl-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sitepark / oparl-client example snippets


use SP\OparlClient\Core\Query\Created;
use SP\OparlClient\OparlClient;
use SP\OparlClient\V1\Objects\OparlSystem;

$client = new OparlClient();

// the system object is the entry point of every OParl endpoint
$system = $client->get('https://oparl.example.org/', OparlSystem::class);

// a system lists its bodies (usually one municipality)
$body = $system->getBody()->get()->getData()[0];

// meetings of that body created since 2024-01-21; further pages are fetched while iterating
$since = new DateTimeImmutable('2024-01-21 00:00:00', new DateTimeZone('Europe/Berlin'));
$meetings = $body->getMeeting()->withQueryParams(Created::since($since))->get();

foreach ($meetings->all() as $meeting) {
    echo $meeting->getName(), ' ', $meeting->getStart()?->format('d.m.Y H:i'), "\n";
}

$client = new OparlClient(
    httpClient: $psr18Client,
    requestFactory: $psr17Factory,
    userAgent: 'my-app/1.0 (+https://example.org/contact)',
    logger: $psr3Logger,
);

use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Psr18Client;

$client = new OparlClient(
    httpClient: new Psr18Client(HttpClient::create([
        'timeout' => 10,         // seconds of inactivity
        'max_duration' => 60,    // seconds for the whole request
        'max_redirects' => 5,
        // 'proxy' => 'http://proxy.example.org:3128',
    ])),
);

use GuzzleHttp\Client as GuzzleClient;
use Http\Client\Common\Plugin\RedirectPlugin;
use Http\Client\Common\PluginClient;

$client = new OparlClient(
    httpClient: new PluginClient(
        new GuzzleClient([
            'connect_timeout' => 5,
            'timeout' => 60,
            // 'proxy' => 'http://proxy.example.org:3128',
        ]),
        [new RedirectPlugin()],
    ),
);

$body = $client->get($bodyUrl, OparlBody::class);

$object = $client->getAny($someUrl);
if ($object instanceof OparlMeeting) {
    // ...
}

$consultation = $client->get($consultationUrl, OparlConsultation::class);
$reference = $consultation->getMeeting();  // OparlReference<OparlMeeting>

$meetingUrl = $reference->getUri();        // only the URL, no request
$meeting = $reference->get();              // request

foreach ($paper->getOriginatorPerson() ?? [] as $originator) {
    $person = $originator->get();
}

$page = $system->getBody()->get();          // OparlList<OparlBody>

$page->getData();                            // the elements of this page
$page->getPagination()->getTotalElements();  // optional, depending on the server

if ($page->hasNextPage()) {
    $next = $page->fetchNextPage();
}

// all elements of all pages; further pages are fetched while iterating
foreach ($page->all() as $body) {
    // ...
}

$papers = [];
foreach ($body->getPaper()->get()->all() as $paper) {
    $papers[$paper->getId()] = $paper;  // a later copy replaces an earlier one
}

use SP\OparlClient\Core\Query\Limit;
use SP\OparlClient\Core\Query\Modified;
use SP\OparlClient\Core\Query\OmitInternal;

$date = new DateTimeImmutable('2024-08-16 00:00:00', new DateTimeZone('Europe/Berlin'));

$papers = $body->getPaper()
    ->withQueryParams(Modified::since($date), OmitInternal::true(), Limit::of(100))
    ->get();

$lastRun = ...;                       // DateTimeImmutable of the last complete run
$thisRun = new DateTimeImmutable();   // taken before the run, so no change during the run is missed

// a safety margin covers clock differences between client and server
$since = $lastRun->modify('-5 minutes');

$papers = $body->getPaper()
    ->withQueryParams(Modified::since($since), OmitInternal::true())
    ->get();
foreach ($papers->all() as $paper) {
    if ($paper->isDeleted()) {
        $repository->delete($paper->getId());
    } else {
        $repository->save($paper);
    }
}
$lastRun = $thisRun;

$faxNumber = $person->getAdditionalProperty('BeispielHersteller:faxNumber');
$all = $person->getAdditionalProperties();

$memberships = $person->getMembership();                // only the valid embedded objects
$original = $person->getAdditionalProperty('membership'); // e.g. ["https://…/membership/1"]

try {
    $body = $client->get($bodyUrl, OparlBody::class);
} catch (OparlConnectionException $e) {
    // try again later
} catch (OparlHttpException $e) {
    $logger->error($e->getStatusCode() . ' for ' . $e->getUri());
} catch (OparlException $e) {
    $logger->error($e->getMessage());
}

$meeting = new OparlMeeting(id: 'https://oparl.example.org/meeting/1', name: 'Ratssitzung');

$json = json_encode($meeting, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

$json = json_encode($meeting);
// ...
$meeting = $client->fromJson($json, OparlMeeting::class);
$page = $client->listFromJson($pageJson, OparlMeeting::class);