メニュー

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?

Twilio SMS PHP Quickstart without Composer

In just a few lines of code, your PHP application can send, receive, and reply to text messages with Twilio Programmable SMS.

This PHP SMS Quickstart shows you how to use our Communications REST API, the Twilio PHP helper library, and ngrok to expose your local development server to Twilio. We'll install all dependencies manually without using a package manager.

We highly suggest trying Composer for package and dependency management in your PHP web applications. If interested, try our standard PHP SMS Quickstart.

In this quickstart, you'll learn how to:

  1. Twilioにサインアップし、SMS機能に対応した電話番号を購入する
  2. Check and install any prerequisites manually
  3. 最初のSMSを送信する
  4. メッセージを送受信できるように開発環境をセットアップする
  5. テキストメッセージを受信する
  6. SMSを返送することで受信メッセージに返信する
Let's do it – show me how!

Already have a Twilio account and SMS-capable number? Go ahead and skip this section.

You can sign up for a free Twilio trial account here.

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test messages to your phone from your Twilio account while in trial mode.
  • Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
  • Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, authentication token, find a Twilio phone number, and more.

If you don't currently own a Twilio phone number with SMS functionality, you'll need to purchase one. After navigating to the Buy a Number page, check the "SMS" box and click "Search."

SMS対応Twilio電話番号を購入する

すると、利用可能な電話番号と、その機能の一覧が表示されます。 お好みに合った番号が見つかったら「購入」をクリックすると、それがアカウントに追加されます。

SMS機能対応の電話番号を選択します。

Sounds excellent! Let's do some installin'...

If you already have PHP and the Twilio PHP Helper Library installed in your working directory, feel free to skip this step and move on to sending your first text message.

To send your first SMS, let's make sure you're set up with PHP and able to install Twilio's PHP Helper library. This quickstart will show you how to install packages manually.

When doing web development in PHP, we strongly suggest using Composer for package management. You can try the standard PHP SMS Quickstart if interested.

PHPをインストールする

MacまたはLinux、Unixマシーンをお使いの場合、PHPはすでにインストールされている可能性があります。 使い慣れたターミナルで下記を実行します:

php --version

If not installed, follow the PHP installation instructions.

Windowsマシーンをお使いの場合は、PHPのインストールを行うには公式のPHPチュートリアルに従ってください。

Many versions of PHP 5.x and PHP 7.x will work for this quickstart, but please pay careful attention to supported PHP releases. Always update your un-supported versions when doing web development as older versions will not receive security updates.

PHPヘルパーライブラリをインストールする

You'll need to install the Twilio PHP Helper Library in the directory where you will complete the quickstart.

  1. Create a new project directory, or navigate to your project's working directory.
  2. In that directory, download the most recent version of the Twilio PHP Helper Library from Github.
  3. Unzip the PHP helper library in the directory.
I've finished with the prerequisites! Let's send some SMS...

PHPでSMSを送信する

Now that we have PHP and twilio-php-master installed manually, we can make a single API request and send an outbound text message from a Twilio phone number.

Create and open a new file called send_sms.php and type or paste in this code sample:

        
        
        
        
        Send an outbound SMS using PHP and the Twilio PHP Helper Library installed without Composer.

        Send an SMS with PHP

        Send an outbound SMS using PHP and the Twilio PHP Helper Library installed without Composer.

        プレースホルダーのTwilioクレデンシャルを置き換える

        In the code, switch the placeholders in account_sid and auth_token with your Twilio credentials. Visit https://www.twilio.com/console to find your unique Account SID and Auth Token to substitute.

        Reveal your auth token by clicking 'view':

        View your authentication token in the Twilio console

        Replace the value for account_sid and auth_token with your unique values.

        Note: While it's easy to hardcode your credentials in a file for a quickstart, use environment variables to keep them secret in production. Check out how to set environment variables for more information, and see the code comments for an example of how to read them in PHP. This repo is also an excellent resource for dealing with environment variables.

        Replace the 'twilio_number'

        Earlier, you purchased an SMS-enabled phone number. Paste that number into the twilio_number variable using E.164 formatting:

        [+][country code][phone number including area code]

        たとえば、+18005551212などです。

        create() 呼び出し内の「To」番号を置き換える

        The first value in the API call to create() is the outgoing phone number, currently set to +15558675310. This can be any phone number that can receive texts... but use a number you control to witness the magic! As above, use E.164 formatting for this number.

        ファイルを保存してスクリプトを実行します:

        php send_sms.php
        

        Assuming all the values are correct, you should already see the SMS from your Twilio number on your phone!

        If you are on a Twilio Trial account, your SMS messages are limited to phone numbers that you have verified with Twilio. You can verify phone numbers via the Twilio Console's Verified Caller IDs.

        Are your customers in the U.S. or Canada? You can also send MMS messages by adding just a single line of code. Check out this guide to sending MMS to see how.

        That SMS was incredible! What’s next?

        PHPでSMSを受信、返信する

        When someone sends an SMS to your Twilio phone number, Twilio makes an HTTP request to your server asking for instructions on what to do next. For this quickstart, we’ll reply to the sender with a note about how we're sending our SMS reply.

        We'll again use the Twilio PHP Library, then use PHP's built-in development webserver in combination with ngrok to instruct Twilio how to handle the message. Create a new file reply_sms.php in the same directory as send_sms.php, open it, then copy and paste or type the following code.

        (メモ: もし同じディレクトリーを使用しない場合は、上記のPHPヘルパーライブラリーのインストール手順に従ってください)

              
              
              
              
              Reply to an inbound SMS using the Twilio PHP Helper Library installed manually and TwiML.

              Reply to a SMS Using PHP

              Reply to an inbound SMS using the Twilio PHP Helper Library installed manually and TwiML.

              ファイルを保存し、下記のコマンドでPHPの開発用サーバーを開始します:

              php -S localhost:8000
              

              使い慣れたブラウザーで、 http://localhost:8000/reply_sms.php にアクセスします。

              If all went smoothly, you should see XML in your browser with our message. And, yes, that's all the code needed - there are just a few more steps before everything is ready to go. Next, let's expose this endpoint to Twilio.

              I'm getting excited now - let's show PHP to Twilio!

              TwilioからPHPアプリケーションへアクセスできるようにする

              Most Twilio services use webhooks to communicate with your application. When Twilio receives an SMS, it reaches out to a specific URL in your application, and you respond with instructions on how to handle a response.

              The small piece of code in reply_sms.php is an example of one instruction you can use in response - but this sadly isn't yet accessible from the outside world, only in your local environment.

              While there are many ways to make this code public (such as deploying to an external host), we recommend a tool called Ngrok. When you start Ngrok, it provides a unique URL on the ngrok.io domain and then forwards incoming requests to your local development environment.

              このアーキテクチャーは、下記のように簡略化できます:

              Twilioがローカルサーバーにアクセスできるようにするために、ngrokがどのように役に立つのか

              まだNgrokをお使いでない場合、ダウンロードページにアクセスしてお使いのプラットフォーム用のものをインストールしてください。

              If you're working on a Mac or Linux, you're all set after decompression. If you're on Windows, we have a guide on how to install and configure ngrok on Windows. For more info on ngrok, including some great tips and tricks, check out our this in-depth blog post.

              ダウンロードインストールが済んだら、新しいターミナルのタブまたはウィンドウを開いて(すなわち開発サーバーは実行中のままにして)、下記のコマンドを使用しNgrokを開始します:

              ./ngrok http 8000
              

              下記のような出力がご覧いただけるでしょう。

              ngrokサーバーのターミナル出力

              この出力から公開URLをコピーしブラウザーにペースト、末尾にreply_sms.phpを追加します。 先の手順と同じXMLファイルが表示されるはずです。 今回のこの出力は外部からでもアクセス可能です。

              素晴らしいでしょう? さて、あとはTwilioに参照先を伝えるだけです。

              My server is public now. Let's show Twilio where to look!

              Configure your Twilio Number Webhook URL

              Now, you configure your Twilio phone number to call your webhook URL when a new message is received.

              1. Log into Twilio.com and go to the Console Numbers page
              2. Click the SMS-enabled phone number (the one we sent messages with originally)
              3. Under the Messaging sub-section, make sure the default “CONFIGURE WITH” is set to "Webhooks/TwiML."
              4. In the A MESSAGE COMES IN section, select "Webhook" and paste in the ngrok URL followed with /reply_sms.php

              SMS Webhookを構成する

              変更を保存すれば、準備は万端です!

              SMSを送信しアプリケーションをテストする

              これですべての設定が完了したので、いよいよテストしてみましょう。

              お使いの携帯電話からTwilio電話番号にテキストメッセージを送信します(日本から米国のTwilio番号にSMSを送信すると高額な送信量が携帯キャリアーによって課されるため、じゅうぶんご注意ください)。 ほどなくして、いくつかのことが起こるのを目撃することになるでしょう -

              1. お手元のNgrokコンソールに、HTTPリクエストが現れます。
              2. PHPの開発サーバーが新しい接続を記録します
              3. Twilio will forward your response as an SMS text message!
              It worked, nice! What's next?

              関連トピック

              これで、PHPを使用したSMSとMMSのテキストメッセージ送受信の基本が身についたので、下記のリソースがお役にたつかもしれません。

              弊社一同、皆さんがこのクイックスタートをお楽しみいただくことを望みます。 そして皆さんがこれから開発されるものを目にする機会を心待ちにしています!

              Rate this page:

              ヘルプが必要ですか?

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