Building a weather forecasts CLI app with Laravel7

  • Obtain a valid API KEY from HERE Developer portal
  • Create a new Laravel project
  • Write the custom command
  • Test the new command

Obtain a valid api key

We are going to consume this API so we need an api key.

The projects page of HERE Developer portal
The REST section in project detail page

Create new Laravel project

In order to create a new Laravel project I suggest you to install Laravel command that helps you on some tasks for example for creating a new project.

composer global require laravel/installer
laravel new weather
cd weather

Write the custom command

Now you have your new Laravel project and you have your API Key for consuming Weather API.

php artisan list

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.

For example to see the list of the available command you can digit:

php artisan list
php artisan weather:forecast "Venice Italy"
  • set your API Key in .env file
  • create a new command
  • define the signature of the command (weather:forecast) with the argument (the name of location)
  • define the description (useful for the user that want use your command)
  • implement the logic that handles your command (handle function)

Set your API Key

In .env file you have a lot of parameter (database, cache, queue etc).


Create the new command

In order to create a new command you need to execute:

php artisan make:command WeatherForecast
  • the attribute signature is for the definition of the command and the parameters;
  • the attribute description is for the help in the command line;
  • the method __construct is for the initialisation of the command (when is instanced);
  • the method handle when the command is execute. It contains the “logic” of the command. This is the main method.
namespace App\Console\Commands;
use Illuminate\Console\Command;
// Step 001: Include the Http wrapper
use Illuminate\Support\Facades\Http;
class WeatherForecast extends Command
* The name and signature of the console command.
* @var string
// Step 002: definition of the signature
protected $signature = 'weather:forecast {location=Berlin}';
* The console command description.
* @var string
// Step 003: description of the command
protected $description = 'weather:forecast
{location : the name of the location}
* Create a new command instance.
* @return void
public function __construct()
* Execute the console command.
* @return mixed
public function handle()
// Step 004 retrieving parameters, location and API key
$locationName = $this->argument('location');
$apiKey = env("HERE_API_KEY", "");
// Step 005: set the base URL for consuming API
$baseUrl = "";
// Step 006: preparing query string for API
$params = array(
'product' => 'forecast_7days',
'apiKey' => $apiKey,
'name' => $locationName,
//"language" => "it",
"metric" => "true"
// Step 007: composing the absolute URL
$url = "${baseUrl}?". http_build_query($params);
// Step 008: Calling API
$response = Http::get($url);
// Step 009: check if everything is fine
if ($response->successful()) {
// Step 010: retrieving JSON (in array format)
$j = $response->json();
// Step 011: access to forecast information (is an array)
$forecast = $j["forecasts"]["forecastLocation"]["forecast"];
// Step 012: looping the forecasts
foreach ($forecast as $key => $value) {
// Step 013: output of each forecast
$this->info($value["daySegment"]. " ". $value["description"]. " - Temperature: " . $value["temperature"] . " - Date: ". $value["utcTime"]);
} else {
// Step 014: managing some errors
if ($response->clientError()) {
$this->error("Error performing request: ".$response->getStatusCode());
} elseif ($response->serverError()) {
$this->error("Error from Server: ".$response->getStatusCode());
} else {
$this->error("Error! ".$response->getStatusCode());
  • Step 001: included the new Http wrapper shipped with Laravel 7;
  • Step 002: defined the signature as weather:forecast and the parameter for the name of location (the city or the place);
  • Step 003: description of the command, useful for the help ( — help);
  • Step 004: we retrieved location name from the command line and the HERE API Key from the .env file;
  • Step 005: define the base URL for the API. This is the new version and the format is JSON;
  • Step 006: prepared the query string, we defined product as forecast_7days (we want forecasting for the next 7 days);
  • Step 007: composed the absolute URL (we used http_build_query PHP native function);
  • Step 008: we performed the API call;
  • Step 009: checked the HTTP error ($response->successful());
  • Step 010: retrieved the JSON structure of the response;
  • Step 011: the forecast is [“forecasts”][“forecastLocation”][“forecast”];
  • Step 012: we persed the array of forecast (multiple forecast for the next 7 days);
  • Step 013: we wrote the output on the console via $this->info method;
  • Step 014: we checked also the error (client and server).
php artisan weather:forecast "Venice Italy"



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Roberto Butti

Roberto Butti


I’m technophile. Vuejs and Laravel enthusiast! #vuejs #laravel. I love #coding