PHP code example of philwc / dark-sky

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

    

philwc / dark-sky example snippets




$client = philwc\DarkSky\ClientFactory::get(
    getenv('SECRET_KEY')
);

$request = \philwc\DarkSky\Entity\ForecastRequest::fromArray([
    'latitude' => 53.4084,
    'longitude' => 2.9916,
]);

$weather = $client->retrieve($request);

echo $weather->getCurrently()->getSummary() . PHP_EOL; // Mostly Cloudy
echo $weather->getCurrently()->getIcon()->toString() . PHP_EOL; // partly-cloudy-day
echo $weather->getCurrently()->getTemperature()->toFloat() . PHP_EOL; // 17.71
echo $weather->getCurrently()->getTemperature()->toString() . PHP_EOL; // 17.71 °F



$log = new Monolog\Logger('test');
$log->pushHandler(new Monolog\Handler\ErrorLogHandler());

philwc\DarkSky\ClientFactory::setLogger($log);

$client = philwc\DarkSky\ClientFactory::get(
    getenv('SECRET_KEY'), 
    new Cache\Adapter\PHPArray\ArrayCachePool(), 
    new philwc\DarkSky\ClientAdapter\GuzzleAdapter()
);

$request = \philwc\DarkSky\Entity\ForecastRequest::fromArray([
    'latitude' => 53.4084,
    'longitude' => 2.9916,
    'parameters' => ['lang' => 'en', 'units' => 'si']
]);
$weather = $client->retrieve($request);

echo $weather->getCurrently()->getSummary() . PHP_EOL; // Mostly Cloudy
echo $weather->getCurrently()->getIcon()->toString() . PHP_EOL; // partly-cloudy-day
echo $weather->getCurrently()->getTemperature()->toFloat() . PHP_EOL; // 17.71
echo $weather->getCurrently()->getTemperature()->toString() . PHP_EOL; // 17.71 °C

// This second call will now be retrieved from the cache
$weather = $client->simpleRetrieve(53.4808, 2.2426, ['units'=>'si', 'lang' => 'en']);

echo $weather->getCurrently()->getSummary() . PHP_EOL; // Mostly Cloudy
echo $weather->getCurrently()->getIcon()->toString() . PHP_EOL; // partly-cloudy-day
echo $weather->getCurrently()->getTemperature()->toFloat() . PHP_EOL; // 17.71
echo $weather->getCurrently()->getTemperature()->toString() . PHP_EOL; // 17.71 °C

$client = philwc\DarkSky\ClientFactory::get(
    getenv('SECRET_KEY'),
    new Cache\Adapter\PHPArray\ArrayCachePool()
);

$client->setForecastTTL(120);

$request = \philwc\DarkSky\Entity\ForecastRequest::fromArray([
    'latitude' => 53.4084,
    'longitude' => 2.9916,
]);

$weather = $client->retrieve($request);

// This second call will now be cached for 120s
$weather = $client->retrieve($request);

 $client = philwc\DarkSky\ClientFactory::get(
     $secretKey,
     new Cache\Adapter\PHPArray\ArrayCachePool(),
     new philwc\DarkSky\ClientAdapter\SimpleAdapter()
 );
 
 $requestCollection = new \philwc\DarkSky\EntityCollection\RequestCollection();
 $manchesterRequest = \philwc\DarkSky\Entity\ForecastRequest::fromArray([
     'latitude' => 53.4808,
     'longitude' => 2.2426,
     'parameters' => ['lang' => 'en', 'units' => 'si']
 ]);
 
 $requestCollection->add($manchesterRequest);
 $liverpoolRequest = \philwc\DarkSky\Entity\ForecastRequest::fromArray([
     'latitude' => 53.4084,
     'longitude' => 2.9916,
     'parameters' => ['lang' => 'en', 'units' => 'si']
 ]);
 $requestCollection->add($liverpoolRequest);
 
 $weatherCollection = $client->retrieve($requestCollection);
 
 foreach ($weatherCollection as $weather) {
     echo $weather->getCurrently()->getSummary() . PHP_EOL;
     echo $weather->getCurrently()->getIcon()->toString() . PHP_EOL;
     echo $weather->getCurrently()->getTemperature()->toFloat() . PHP_EOL;
     echo $weather->getCurrently()->getTemperature()->toString() . PHP_EOL;
 }