Imagine you’re on vacation and have limited or no access to the internet. Maybe you would like to go surfing or skiing and your access to a forecast is non-existent. This type of scenario is a very good use case of forecasting weather using SMS and a server-side language such as PHP. That's what we are going to learn how to build in this tutorial.

Technical Requirements

Set Up Our Development Environment

To kick start our project we will need to create a project directory for it. You may use Weather-App as this is what I will be using too. In the folder create the following files.

  • .env
  • webhook.php
  • functions.php

Next, we need to set up our .env file.

NOTE: The .env file is a hidden file we create on our servers to store secret and private keys it is not accessible by the browser we can store our API keys or credentials in it and access from anywhere in our app on the server side.

To allow the support of .env configs in our app we need to install a package using Composer. Run this command in your terminal in the project directory:

$ composer require vlucas/phpdotenv

We also need to install one more package from Twilio. There are many libraries that enable us to access the Twilio API more efficiently than traditional cURL requests. For this project, we need to install the Twilio PHP SDK Run the following command in your terminal in the project directory.

$ composer require twilio/sdk

Setup Twilio Credentials & Phone Number

If you have not already done so, take a few minutes and head to your Twilio account to create a new number. We need to get the following information from Twilio to allow is send and receive SMS from our project

Once we have these items, we can add them to our .env file.

# TWILIO CREDENTIALS
TWILIO_ACCOUNT_SID=""
TWILIO_AUTH_TOKEN=""
TWILIO_PHONE_NUMBER="+1999999999"

Create a Function to get Weather Updates

To get weather data we need to use the OpenWeatherMaps API services which we will integrate later into our app for SMS forecasting. In your browser, head to the OpenWeatherMaps website to create an account after creating an account, click on the “API keys” tab to see your API keys in your dashboard.

Copy the key you have generated or the default one, and put in your .env file like so.

# OPEN WEATHER MAP API CREDENTIALS
OPEN_WEATHER_API_KEY=""
#The root API URl
OPEN_WEATHER_API_URL="http://api.openweathermap.org/data/2.5/weather"

Now that we have generated our API Keys, let use them to connect to the OpenWeatherMap service. The functions.php file we created earlier will contain the function that will take the user’s command, process it and send the location name to OpenWeatherMap’s API. Add the following code to the functions.php file.

<?php
require __DIR__ . "/vendor/autoload.php";
$dotenv = Dotenv\Dotenv::create(__DIR__);

$dotenv->load();

function getWeatherData($command)
{
 //clean user data
 $location = explode("-", $command);
 if ($location[0] === 'WN') {
 try {
 $data = [
 'q' => $location[1],
 'appid' => getenv("OPEN_WEATHER_API_KEY"),
 'units' => 'metric'
 ];
 $url = getenv("OPEN_WEATHER_API_URL");
 $query_url = sprintf("%s?%s", $url, http_build_query($data));
 $curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, $query_url);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($curl, CURLOPT_TIMEOUT, 10);
 curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
 curl_setopt($curl, CURLOPT_HEADER, 0);
 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
 $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
 $result = curl_exec($curl);
 header('Content-type: application/json');
 $weather_data = json_decode($result, true);
 $response = [
 'weather' => $weather_data['weather'][0]['description'],
 'temp' => $weather_data['main']['temp']
 ];
 return $response;
 curl_close($curl);
 } catch (exception $e) {
 print_r($e);
 }
 }
}

Create a Twilio Webhook

To receive SMS weather forecast requests from users we will need to create a webhook. Twilio has a webhook feature that sends a response to an endpoint whenever your phone number receives a message.

We will need to create our own webhook to receive forecasts from users. In the webhook.php file, copy and paste the following code into it.

<?php
require __DIR__ . "/vendor/autoload.php";
require_once('functions.php');
use Twilio\Rest\Client;
$dotenv = Dotenv\Dotenv::create(__DIR__);
$dotenv->load();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 $message = $_REQUEST['Body'] ?? '';
 $from = $_REQUEST['From'] ?? '';
 if ($message !== '') {

 $account_sid = getenv("TWILIO_ACCOUNT_SID");
 $auth_token = getenv("TWILIO_AUTH_TOKEN");
 $twilio_number = getenv("TWILIO_PHONE_NUMBER");

 $weatherData = getWeatherData($message);
 $text = $weatherData['weather'].' Today with '.$weatherData['temp'].' degrees Temprature ';
 $client = new Client($account_sid, $auth_token);
 $client->messages->create($from,
 array(
 'from' => $twilio_number,
 'body' => $text
 )
 );
 }
}

We will now use Ngrok in order to make this webhook accessible through a public URL from our localhost.

From the terminal, run the command:

$ php -S localhost:3000

On a new terminal window, run the command:

./ngrok http 3000

Your terminal window should look like this:

In our Twilio project, we will provide the webhook URL above, followed by the path to our webhook file. For example, mine is:

https://28cf7026.ngrok.io/webhook.php

The final step is hooking our Twilio number to the webhook we created. Navigate to the active numbers on your dashboard and click on the number you would like to use. In the messaging section, where there is an input box labeled “A MESSAGE COMES IN”, replace the URL with our webhook URL and click “Save”!

Testing Our App

Send WN-Lagos to your TWILIO phone number to get the current weather status of Lagos City. You can try your own city by using WN-City Name

Conclusion

Good job! Now we can receive weather updates using SMS. You can implement other features like making users send more commands to get more weather data.

Bonus

Github Repo: https://github.com/ladaposamuel/sms-weather-php