PHP code example of lenorix / laravel-datadis-client
1. Go to this page and download the library: Download lenorix/laravel-datadis-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/ */
lenorix / laravel-datadis-client example snippets
'datadis' => [
'username' => env('DATADIS_USERNAME'), // the NIF, NIE or CIF you registered with
'password' => env('DATADIS_PASSWORD'),
],
use Lenorix\DatadisClient\DatadisClient;
use Lenorix\DatadisClient\Time\Month;
use Lenorix\DatadisClient\Values\Cups;
class ConsumptionController
{
public function __invoke(DatadisClient $datadis)
{
$supply = $datadis->findSupply(Cups::fromString('ES0000000000000000AA0A'));
abort_unless($supply?->isQueryable(), 404);
$result = $datadis->getConsumptionDataOf($supply, Month::of(2026, 7));
foreach ($result->records as $reading) {
echo $reading->start?->format('Y-m-d H:i'), ' ', $reading->consumptionKWh, " kWh\n";
}
}
}
use Lenorix\LaravelDatadisClient\Facades\LaravelDatadisClient as Datadis;
Datadis::getSupplies();
use Lenorix\DatadisClient\Values\Nif;
$holder = Datadis::forHolder(Nif::fromString('12345678Z'));
$holder->getSupplies();
use Lenorix\DatadisClient\Values\Cups;
use Lenorix\DatadisClient\Values\Nif;
$nif = Nif::fromString('12345678Z');
Datadis::newAuthorization($nif); // all your supplies
Datadis::newAuthorization( // or some, for a period
$nif,
new DateTimeImmutable('2026-01-01'),
new DateTimeImmutable('2026-12-31'),
Cups::fromString('ES0000000000000000AA0A'),
);
Datadis::cancelAuthorization($nif);
Datadis::listAuthorization();
use Lenorix\DatadisClient\Time\BillingCycle;
use Lenorix\DatadisClient\Time\BillingPeriod;
$period = BillingCycle::monthlyFrom(15)->lastEndedPeriod(now()); // or BillingPeriod::between($firstDay, $lastDay) with the dates of the invoice
$months = $period->months(); // the months to ask Datadis for
$result = $client->getConsumptionDataOf($supply, $months[0], end($months));
$period->totalKWh($result->records); // the exact total, as text
$period->isCoveredBy($result->records); // false while Datadis has not published up to its last day
use Lenorix\DatadisClient\PublicApi\Community;
use Lenorix\DatadisClient\PublicApi\PublicSearchQuery;
use Lenorix\DatadisClient\PublicApiClient;
$query = new PublicSearchQuery(new DateTimeImmutable('2026-01-01'), new DateTimeImmutable('2026-01-31'), [Community::Madrid]);
app(PublicApiClient::class)->apiSearch($query); // or Datadis::publicApi()
$until = $client->consumptionDataBlockedUntilOf($supply, $month); // ?DateTimeImmutable, null when it may be sent now
use Lenorix\DatadisClient\Guard\LedgerEventKind;
use Lenorix\LaravelDatadisClient\Events\DatadisLedgerChanged;
Event::listen(function (DatadisLedgerChanged $event) {
// $event->kind: Claimed (it is going out), Released (it never left, so it may be sent again), Remembered (an import),
// Refused (an attempt within the window holds it)
// $event->account: the name of the account in `datadis-client.accounts`
// $event->key, $event->at (when it was claimed or refused; for Remembered, the time of the imported attempt), $event->endpoint
// $event->lastAttemptAt, $event->availableAt: for a refusal, the attempt that holds the query and when it may go again
});
$supply = $client->findSupply(Cups::fromString($cups));
$consumption = $client->getLatestConsumptionDataOf($supply); // the current month, or the previous one plus it
$maxPower = $client->getLatestMaxPowerOf($supply);
use Lenorix\DatadisClient\Time\Month;
use Lenorix\DatadisClient\Values\Cups;
use Lenorix\LaravelDatadisClient\Facades\LaravelDatadisClient as Datadis;
foreach (SentQuery::where('sent_at', '>', now()->subHours(25))->get() as $sent) {
Datadis::rememberConsumption(
Cups::fromString($sent->cups),
$sent->distributor_code,
$sent->point_type,
Month::fromString($sent->start_month), // YYYY/MM, as Datadis takes it; Month::of($year, $month) otherwise
Month::fromString($sent->end_month),
at: $sent->sent_at,
);
}
use Illuminate\Support\Facades\Http;
use Lenorix\LaravelDatadisClient\Testing\FakesDatadis;
FakesDatadis::fake([
'*/get-max-power*' => Http::response(['maxPower' => [/* rows */], 'distributorError' => []]), // yours first
]);