>本文展示了使用Twilio和Laravel构建天气预报的电话应用程序。 这个两部分的系列侧重于第一部分的语音交互,并在第二部分中添加SMS功能。
>
密钥功能:
宅基地改进(或可比的开发环境)。 请参阅链接的Sitepoint和Vagrant Resources以进行设置帮助。 PHP开发环境书籍还链接了以进行进一步学习。
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"
路由(
routes/web.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'); });
创建一个类以封装业务逻辑。 该课程使用GeOnames的地理数据来处理来自国家气象服务API(app/Services/WeatherService.php
)的检索天气数据。 实施缓存以提高性能。 关键方法为>,WeatherService
,https://api.weather.gov
,getWeather
,retrieveNwsData
,getPoint
。
getTimeZone
getTimestamp
getCredits
管理用户交互,使用app/Http/Controllers/VoiceController.php
处理天气数据检索。 方法包括,VoiceController
,WeatherService
和showEnterZipcode
。 这些方法返回twilio twiml响应,引导用户通过呼叫。showZipcodeWeather
showDayWeather
showCredits
>
>环境变量(.env
):配置您的twilio auth token(TWILIO_APP_TOKEN
)和geonames username(GEONAMES_USERNAME
>选项正确处理主机标头。>
-host-header
>生产注意事项:
建议用于生产部署的https端点。结论:
>原始文本的FAQ部分广泛,涵盖了将Laravel与不同的通信服务集成的各个方面(Twilio,Plivo,Sinch)。 由于这个问题仅要求重写提供的文本,因此我省略了本节的简洁性。 但是,如果您愿意包含此部分,请让我知道。>
以上是你好,拉拉维尔?通过电话与PHP通信!的详细内容。更多信息请关注PHP中文网其他相关文章!