PHP code example of pastclimate / pastclimate-php

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

    

pastclimate / pastclimate-php example snippets




astClimate\Client;

$client = new Client(); // reads PASTCLIMATE_API_KEY

$response = $client->getDaily('gb/london', '2024-01-01', '2024-01-31');

foreach ($response->data as $day) {
    echo "{$day->date}: {$day->daily_min_temp} to {$day->daily_max_temp}\n";
}

echo "Used {$response->creditsUsed()} credits, {$response->creditsRemaining()} left\n";

$client = new Client(
    apiKey: '...',                          // default: the PASTCLIMATE_API_KEY environment variable
    baseUrl: 'https://api.pastclimate.com', // default: PASTCLIMATE_BASE_URL, then this value
    timeout: 30.0,                          // seconds per attempt
    maxRetries: 2,                          // retries after the first try
    maxRetryDelay: 60.0,                    // longest wait the client will sleep between tries
);

use PastClimate\Place;

// 1. A location id: country code, slash, place slug. A plain string works.
$client->getMonthly('gb/london');
$client->getMonthly(Place::id('gb/london'));

// 2. A latitude and longitude, with the two-letter country they are in.
$client->getMonthly(Place::point(51.5, -0.12, 'gb'));

$found = $client->searchLocations('lon', country: 'gb');
foreach ($found->data as $place) {
    echo "{$place->id} {$place->name}\n";
}

$nearest = $client->nearestLocation(51.5, -0.12, 'gb');
echo "{$nearest->data->id}, {$nearest->data->requested->distance_km} km away\n";

$london = $client->getLocation('gb/london');
echo $london->data->timezone, ' ', $london->data->coverage->last_date, "\n";

use PastClimate\Units;

$daily = $client->getDaily('gb/london', '2024-01-01', '2024-01-31', Units::Imperial);
foreach ($daily->data as $d) {
    echo "{$d->date} {$d->daily_avg_temp} {$daily->meta->units->temperature}\n";
}

$hourly = $client->getHourly('gb/london', '2024-01-15', days: 1);
foreach ($hourly->data as $h) {
    echo $h->timestamp_utc->format('c'), " {$h->temperature}\n";
}

$monthly = $client->getMonthly('gb/london');
$latest = $monthly->data[count($monthly->data) - 1];
echo $latest->monthly_total_rain, "\n";

foreach ($client->getYearly('gb/london')->data as $y) {
    echo "{$y->year} {$y->yearly_avg_temp}\n";
}

$normals = $client->getNormals('gb/london', month: 7);
echo $normals->data->monthly[0]->avg_temp, ' ', $normals->data->day_of_year[0]->rain_prob, "\n";

foreach ($client->getRecords('gb/london')->data as $record) {
    echo "{$record->metric}: {$record->value} {$record->unit} on {$record->occurred_on}\n";
}

foreach ($client->getCoverage(country: 'gb')->data->countries as $c) {
    echo "{$c->country}: {$c->first_year} to {$c->last_date}\n";
}

$me = $client->getMe()->data;
echo $me->plan->name, ' ', $me->credits->used_this_month, ' ', $me->rate_limit->per_minute, "\n";

$response = $client->getRecords('gb/london', Units::Imperial);
echo $response->meta->units->temperature; // F

$csv = $client->getDailyCsv('gb/london', '2023-01-01', '2023-12-31');
file_put_contents('london-2023.csv', $csv->csv);
echo $csv->creditsUsed();

$response = $client->getMonthly('gb/london');

$response->creditsUsed();          // credits this call used
$response->creditsRemaining();     // credits you can still spend (may be null)
$response->meta->methodology_version;

$limit = $response->rateLimit;     // the per-minute request limit
$limit->limit;                     // requests allowed per minute
$limit->remaining;                 // requests left in this minute
$limit->reset;                     // seconds until the oldest request leaves the minute window

use PastClimate\Exception\{ApiException, ConnectionException, NotFoundException, QuotaExceededException, RateLimitedException};

try {
    $client->getDaily('gb/london', '1900-01-01', '1900-01-31');
} catch (NotFoundException $e) {
    echo "No data: {$e->errorCode} - {$e->getMessage()}\n"; // e.g. not_published; see getCoverage()
} catch (QuotaExceededException $e) {
    echo "Out of credits. Upgrade: {$e->upgradeUrl}\n";
} catch (RateLimitedException $e) {
    echo "Slow down. Wait {$e->retryAfter} seconds.\n";
} catch (ApiException $e) {
    echo "{$e->status} {$e->errorCode}: {$e->getMessage()} ({$e->docsUrl})\n";
} catch (ConnectionException $e) {
    echo "Network problem: {$e->getMessage()}\n";
}

use PastClimate\Client;
use PastClimate\Http\Psr18Transport;

$client = new Client(
    apiKey: getenv('PASTCLIMATE_API_KEY') ?: null,
    transport: new Psr18Transport($psr18Client, $psr17RequestFactory),
);
sh
export PASTCLIMATE_API_KEY=your_key_here