PHP code example of chargemap / ocpi-protocol

1. Go to this page and download the library: Download chargemap/ocpi-protocol 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/ */

    

chargemap / ocpi-protocol example snippets


use Chargemap\OCPI\Versions\V2_1_1\Server\Emsp\Sessions\Put\OcpiEmspSessionPutRequest;
use Chargemap\OCPI\Versions\V2_1_1\Server\Emsp\Sessions\Put\OcpiEmspSessionPutResponse;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/** @var RequestInterface $httpRequest */
$sessionPutRequest = new OcpiEmspSessionPutRequest($httpRequest, 'NL', 'TNM', '101');
$session = $sessionPutRequest->getSession();

// Some code...

$sessionPutResponse = new OcpiEmspSessionPutResponse($session);
/** @var ResponseInterface $response */
$response = $sessionPutResponse->getResponseInterface();

use Chargemap\OCPI\Versions\V2_1_1\Server\Emsp\Tokens\Get\OcpiEmspTokenGetRequest;
use Chargemap\OCPI\Versions\V2_1_1\Server\Emsp\Tokens\Get\OcpiEmspTokenGetResponse;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/** @var RequestInterface $httpRequest */
$tokenGetRequest = new OcpiEmspTokenGetRequest($httpRequest);
$tokens = [];
$tokenCount = 0;

//Fetch tokens from database...

$tokenGetResponse = new OcpiEmspTokenGetResponse($tokenGetRequest, $tokenCount, count($tokens));
foreach ($tokens as $token) {
    $tokenGetResponse->addToken($token);
}
// X-Total-Count, X-Limit and Link headers are already set in $response
/** @var ResponseInterface $response */
$response = $tokenGetResponse->getResponseInterface();

use Chargemap\OCPI\Versions\V2_1_1\Client\Locations\GetListing\GetLocationsListingRequest;
use Chargemap\OCPI\Versions\V2_1_1\Common\Models\Location;
use Chargemap\OCPI\Common\Client\OcpiClient;
use Chargemap\OCPI\Common\Client\OcpiConfiguration;
use Chargemap\OCPI\Common\Client\OcpiEndpoint;
use Chargemap\OCPI\Common\Client\OcpiVersion;
use Chargemap\OCPI\Common\Models\BaseModuleId;

$ocpiClient = new OcpiClient(
            (new OcpiConfiguration($supervisorAuthString))
                ->withEndpoint(new OcpiEndpoint(
                    OcpiVersion::V2_1_1(),
                    BaseModuleId::LOCATIONS(),
                    new Uri('ocpi/cpo2.0/locations'))
                )
        );
$getLocationListingRequest = (new GetLocationsListingRequest())
                                ->withOffset(0)
                                ->withLimit(100)
                                ->withDateFrom($dateFrom)
                                ->withDateTo($dateTo);
$locationResponse = $ocpiClient->V2_1_1()->locations()->getListing($getLocationListingRequest);
/** @var Location[] $locations */
$locations = $locationResponse->getLocations();
//Some code...

use Chargemap\OCPI\Versions\V2_1_1\Client\Locations\GetListing\GetLocationsListingRequest;
use Chargemap\OCPI\Common\Client\OcpiClient;
use Chargemap\OCPI\Common\Client\OcpiConfiguration;
use Chargemap\OCPI\Common\Client\OcpiEndpoint;
use Chargemap\OCPI\Common\Client\OcpiVersion;
use Chargemap\OCPI\Common\Models\BaseModuleId;

$ocpiClient = new OcpiClient(
            (new OcpiConfiguration($supervisorAuth))
                ->withEndpoint(new OcpiEndpoint(
                    OcpiVersion::V2_1_1(),
                    BaseModuleId::LOCATIONS(),
                    new Uri('ocpi/cpo2.0/locations'))
                )
        );
$getLocationListingRequest = (new GetLocationsListingRequest())
                                ->withOffset(0)
                                ->withLimit(100)
                                ->withDateFrom($dateFrom)
                                ->withDateTo($dateTo);
do {
    $locationResponse = $this->ocpiClient->V2_1_1()->locations()->getListing($getLocationListingRequest);
    
    //Some code...     
    
    //Next request will update its limit and offset values
    $getLocationListingRequest = $locationResponse->getNextRequest();
} while ($getLocationListingRequest !== null);

use Chargemap\OCPI\Common\Server\Errors\OcpiGenericClientError;
use Psr\Http\Message\ResponseInterface;

$error = new OcpiGenericClientError('Client error');
//Correct payload and HTTP error code is already set
/** @var ResponseInterface $response */
$response = $error->getResponseInterface();