Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now

メニュー

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

SMS and MMS Notifications with PHP and Laravel

Today we're going to get your server to automatically sound the (textual) alarm when something goes wrong. Using PHP and Laravel, we'll send SMS text message server notifications to the phones of all of your server administrators when there's an exception on your site.

Clone the application and head to the application's README to see how to run the app locally.

EMCがいかにしてTwilio SMSを使用して68,000人の従業員にITアラートを送信しているか見てみましょう

はじめましょう!

Click the button below to start the journey.

List Your Server Administrators - And Other Lucky Folks

Here we create a list of people who should be notified if a server error occurs.

唯一必要なデータは、各管理者の phone_number です。

        
        
        
        
        List of administrators which will receive notifications when a server error occurs.

        JSON Administrator List

        List of administrators which will receive notifications when a server error occurs.

        Next up, we'll see how to set up the Twilio client.

        Configure the Twilio Client

        To send a message, we'll need to create a Twilio REST client, which requires reading our TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN from environmental variables.

        The values for your account SID and Auth Token come from the Twilio console:

        コンソール内のTwilioアカウントサマリーセクション

              
              
              
              
              app/Exceptions/Handler.php

              Exception and utility handlers

              app/Exceptions/Handler.php

              Next let's see how to handle application exceptions.

              Expertly Accept Exceptions

              Handle Application Exceptions

              Each time an exception is raised anywhere in a Laravel application an ExceptionHandler will handle it. (Appropriate name!)

              This is where we hook our SMS sending code. It's important to call parent so the framework can do its regular error handling as well.

                    
                    
                    
                    
                    app/Exceptions/Handler.php

                    例外に先立って通知する

                    app/Exceptions/Handler.php

                    Next up let's see how to create a custom message.

                    Create Your Message Text

                    カスタムなアラートメッセージを作成する

                    ここでは、テキストメッセージを介して送信されるアラートメッセージを作成します。

                    You might also decide to include a picture with your alert message... perhaps a screenshot of the application when the crash happened? A meme image to calm everyone down?

                          
                          
                          
                          
                          app/Exceptions/Handler.php

                          Sending a custom alert message

                          app/Exceptions/Handler.php

                          Let's now take a look at how to load the list of administrators.

                          Read the Administrators from the JSON File

                          We read the lucky people from our JSON file and send alert messages to each of them with the private send_message method.

                                
                                
                                
                                
                                app/Exceptions/Handler.php

                                Read the alert message recipients

                                app/Exceptions/Handler.php

                                Now let's look at how to send a text message.

                                Send SMSes Out

                                SMSを送信

                                Twilio REST APIを使用したSMSの送信には、3つのパラメーターが必要です: FromTo、およびBodyです。

                                After the message is sent, we print out the phone number we're texting. US and Canadian phone numbers can also send an image with the message. (Other countries will have a shortened url appended pointing to the image.)

                                      
                                      
                                      
                                      
                                      app/Exceptions/Handler.php

                                      Send out an SMS or MMS

                                      app/Exceptions/Handler.php

                                      これで以上です!

                                      We've just implemented an automated server notification system that can push out server alerts if (ahem, when) anything goes wrong. Now let's look at some other great features you might like to add.

                                      他に構築できるもの

                                      次はどこでしょうか?

                                      PHP and Twilio - such a great combination. Here are just two other excellent tutorials for you to try out:

                                      二要素認証

                                      パスワードに加えてユーザーの携帯電話を検証することで、ログインシステムのセキュリティーを強化します。

                                      アポイントメント リマインダー

                                      近く予定されているアポイントメントがあったときに、お客様にテキストメッセージを送信します。このチュートリアルでは、バックグラウンドジョブからそれを行う方法を示します。

                                      これは役に立ちましたか?

                                      このチュートリアルをご覧いただき、ありがとうございます。 @twilio へツイートいただき、ご意見をお知らせください。

                                      Jarod Reyes Jose Oliveros Agustin Camino Kat King Samuel Mendes Paul Kamp Andrew Baker
                                      Rate this page:

                                      ヘルプが必要ですか?

                                      誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。