Home >Backend Development >PHP Tutorial >Hello, Laravel? Communicating with PHP through Phone Calls!
This article demonstrates building a weather forecast phone application using Twilio and Laravel. This two-part series focuses on voice interaction in part one, with SMS functionality to be added in part two.
Users call a Twilio number, enter a zip code, and receive the current weather forecast. A voice menu allows retrieval of forecasts for specific days of the week.
Key Features:
Prerequisites:
<code class="language-bash">cd ~/Code composer create-project --prefer-dist laravel/laravel Laravel 5.4.* cd Laravel composer require "twilio/sdk:^5.7" composer require "guzzlehttp/guzzle:~6.0"</code>
Development Steps:
routes/web.php
): Define routes for voice interactions:<code class="language-php">Route::group(['prefix' => 'voice', 'middleware' => 'twilio'], function () { Route::post('enterZipcode', 'VoiceController@showEnterZipcode')->name('enter-zip'); Route::post('zipcodeWeather', 'VoiceController@showZipcodeWeather')->name('zip-weather'); Route::post('dayWeather', 'VoiceController@showDayWeather')->name('day-weather'); Route::post('credits', 'VoiceController@showCredits')->name('credits'); });</code>
Service Layer (app/Services/WeatherService.php
): Create a WeatherService
class to encapsulate business logic. This class handles retrieving weather data from the National Weather Service API (https://api.weather.gov
), using geographic data from GeoNames. Caching is implemented for improved performance. The key methods are getWeather
, retrieveNwsData
, getPoint
, getTimeZone
, getTimestamp
, and getCredits
.
Controller (app/Http/Controllers/VoiceController.php
): The VoiceController
manages user interactions, using the WeatherService
to handle weather data retrieval. Methods include showEnterZipcode
, showZipcodeWeather
, showDayWeather
, and showCredits
. These methods return Twilio TwiML responses guiding the user through the call.
Middleware (app/Http/Middleware/TwilioRequestValidator.php
): A custom middleware validates incoming requests from Twilio using the Twilio RequestValidator, ensuring only legitimate requests are processed. CSRF protection is disabled for Twilio webhooks.
Environment Variables (.env
): Configure your Twilio Auth Token (TWILIO_APP_TOKEN
), and GeoNames username (GEONAMES_USERNAME
).
Ngrok: Use Ngrok to expose your local development server to the internet for Twilio webhook calls. Remember to use the -host-header
option to correctly handle the host header.
Twilio Setup: Obtain a Twilio phone number with voice and SMS capabilities. Configure the webhook URLs in your Twilio account settings to point to your Ngrok-exposed routes.
Production Considerations: Switching to HTTPS endpoints is recommended for production deployments.
Conclusion: This article details creating a functional voice-based weather application. Part two will extend this to include SMS functionality. The Github repository (link not provided in original text) will contain the complete code.
FAQs (from original text, reformatted):
The FAQs section from the original text is extensive and covers various aspects of integrating Laravel with different communication services (Twilio, Plivo, Sinch). Since the question only asks for re-writing the provided text, I have omitted this section for brevity. However, if you would like this section included, please let me know.
The above is the detailed content of Hello, Laravel? Communicating with PHP through Phone Calls!. For more information, please follow other related articles on the PHP Chinese website!