メニュー

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?

Programmable Voice Quickstart for PHP without Composer

With very few lines of code, your PHP web application can make and receive phone calls with Twilio Programmable Voice.

This PHP Voice quickstart will teach you to do this using Twilio's Voice REST API, the Twilio PHP helper library, the built-in PHP development web server, and ngrok to expose your local server to Twilio.

We'll install and manage packages manually in this quickstart

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

このクイックスタートでは、下記のことを学んでいきます:

  1. Twilioにサインアップして、音声通話機能を持ったはじめてのTwilio電話番号を入手する
  2. Check and install any PHP prerequisites manually
  3. 通話を発信してMP3ファイルを再生する
  4. 音声通話に応答し、テキストtoスピーチを使用して発信者にメッセージを読み上げる
Show me how to call with PHP!

Already have a Twilio account and voice-enabled Twilio phone number to use? Log in, then jump to the next step.

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 make calls 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 Voice functionality, you'll need to purchase one. After navigating to the Buy a Number page, check the "Voice" box and click "Search."

Buy a voice compatible number with Twilio

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

voice_compatible-number.png

ヘルパーライブラリーのインストールを手助けしてください。

If you already have PHP and the Twilio PHP Helper Library installed locally, you're all set here. Feel free to skip ahead and move on to sending your first text message.

To make your first outbound voice call, let's make sure you're set up with PHP and able to install Twilio's PHP Helper library.

In this quickstart, we'll install the Twilio Helper Library manually. When doing web development in PHP, we strongly suggest using Composer for your package management. If you'd prefer to do that, try our standard PHP Voice Quickstart.

PHPをインストールする

MacまたはLinux / UNIXをご使用の場合は、すでにPHPはインストールされています。 ターミナルで下記を実行してください:

php --version

PHPがインストールされていない場合は、PHP installation instructions (PHPインストールの手順) に従ってください。

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

Many versions of PHP 5.x and PHP 7.x will work for this quickstart, but please pay attention to supported PHP releases. When doing web development, always update PHP to a release which receives security updates.

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

ここで、Twilio PHPヘルパーライブラリーのインストールが必要です。

  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.
Manually installed and ready to go!

PHPで通話を発信する

Now that we have PHP and the PHP Helper Library installed, we can make an outgoing phone call in a single API request. Create a new file called make_call.php and type or paste in this code sample.

        
        
        
        
        Make an outbound phone call with PHP and the Twilio Helper Library installed manually

        Make a Voice Call with PHP

        Make an outbound phone call with PHP and the Twilio Helper Library installed manually

        When you run the code, it will start a phone call between the two numbers included in this code. We added two variables to show you the 'From' and 'To' number. The numbers map like this:

        • (From) / twilio_number: 購入されたTwilio電話番号
        • (To) / to_number: 通話の発信先の番号 (おそらく皆さんの携帯電話番号)

        Inside the array, the url argument points to some TwiML. TwiML is a language Twilio uses to initiate or respond to actions, such as a sequence for a voice call. This particular TwiML causes Twilio to read a message with text to speech then play an MP3 to the 'To' / to_number number.

        Before you can run this code, you need to swap in a few account-specific values.

        プレースホルダーのクレデンシャルをご自身のもので置き換える

        account_sidおよびauth_tokenの固有の値をお使いのTwilioアカウントクレデンシャルに置き換えます。

        To find these, visit https://www.twilio.com/console and log in. On this page, you’ll find your unique Account SID and Auth Token. You can reveal the Auth Token by clicking 'view':

        View your authentication token in the Twilio console

        make_call.phpを開き、account_sidおよびauth_tokenに対応する値をご使用のアカウントの一意な値に置き換えます。

        Note: It's easier to hardcode your credentials in a quickstart, but 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 a reading example. This repo is also an excellent reference for environment variables in PHP.

        Replace the twilio_number

        Earlier, you purchased or found a voice-enabled phone number. Paste the number into the twilio_number variable using E.164 format:

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

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

        to_number 電話番号を置き換える

        Again using E.164 formatting, substitute the phone number which will receive the outgoing call. You should use a personal phone number here so you'll receive the call and hear the magic after running the code.

        If you're using a Twilio trial account, you can only make outgoing calls to phone numbers you have verified with Twilio. You can verify phone numbers via the Twilio Console's Verified Caller IDs. For full trial account limitations, see our guide on how to work with your free Twilio trial account.

        Save the script and invoke it to test an outgoing call. In OSX and *NIX, this looks like:

        php make_call.php
        

        If you substituted everything correctly, you should hear a great message and a brand new song! If it didn't work though, don't ever give up and follow the console prompts.

        Twilio never lets me down! What's next?

        PHPで着信通話に応答する

        When someone calls your Twilio number, Twilio makes an HTTP request to your server to ask how to handle the call. For this quickstart, we’ll reply to the sender and thank them for their phone call using Twilio's text to speech capabilities.

        このステップでは再度TwilioのPHPライブラリーを使用します。 PHPの組み込み済み開発Webサーバーをngrokと組み合わせてTwilioに通話を管理する方法を指示します。

        Create a new file, answer_call.php, in the same directory as make_call.php. Then copy and paste or type the following code.

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

              
              
              
              
              Respond to an inbound voice call with PHP and the Twilio Helper Library installed manually

              Answer a Call with PHP

              Respond to an inbound voice call with PHP and the Twilio Helper Library installed manually

              Save answer_call.php, then start a local PHP development server:

              php -S localhost:8000
              

              In a browser tab open http://localhost:8000/answer_call.php.

              If everything went well, you should see XML in your browser with the message we'd like to speak to incoming calls. That's all the code you need - there are just a few more steps before everything is wired up.

              次に、このエンドポイントをTwilioから参照できるようにしましょう。

              Straightforward. Let's let Twilio talk to us!

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

              Before we can instruct Twilio on how to handle an incoming call, we first need to expose the server to the public. When you run your local development server, the odds are very high it is only accessible locally. But don't worry - we'll show you an easy way to test your server.

              Many Twilio products and services use webhooks to communicate with your application. For example, when Twilio receives an incoming call, it reaches out to a URL you provide for instructions on how to handle the response. The small piece of code in answer_call.php is an example of one instruction you can use to 'speak' back to the caller. However, when you run the server, it will only be exposed to you locally, not to the broader public. We need to figure out a way to fix that.

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

              アーキテクチャーは下記のようになります:

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

              まだNgrokをお使いでない場合、ダウンロードページにアクセスしてお使いのオペレーティングシステム用のものをインストールしてください。

              If you're working on Mac or Linux, you're all set after you decompress it. If you're on Windows, follow our 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 ngrok blog post.

              ngrokインストールのダウンロードが済んだら、新規のターミナルタブまたはウィンドウを開き(すなわち実行中の開発サーバーはそのままにして)、下記のコマンドでこれを起動します:

              ./ngrok http 8000
              

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

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

              この出力から公開URLをコピーしブラウザーにペースト、末尾にanswer_call.phpを追加します。 先の手順と同じXMLファイルが表示されるはずです。 これでこの出力はインターネット接続さえあればどこからでもアクセス可能です。

              これでサーバーを公開できたので、Twilioにサーバーの探し場所を伝えましょう。

              My server is public now. Now, receive phone calls!

              WebhookのURLを設定する

              Now that your server is publically accessible you can configure your Twilio phone number to access your webhook URL.

              1. Visit the Console Numbers page.
              2. Click on your voice-enabled phone number (from before).
              3. Find the Voice & Fax section. Make sure the "Accept Incoming" selection is set to "Voice Calls." The default "Configure With" selection is what you’ll need: "Webhooks/TwiML...".
              4. In the A Call Comes In section, select "Webhook" and paste in the URL you want to use, appending /answer_call.php :

              ngrokを使用した音声通話Webhook

              Save your changes. You're ready to roll!

              アプリケーションをテストする

              ローカル開発環境がまだ実行中で、かつngrokもまだ実行中であれば、お楽しみ、すなわちテストの準備は万端です!

              Make a phone call to your Twilio phone number. You'll see and hear several things happen in short succession:

              1. ngrokコンソールでHTTPリクエストが確認できます
              2. 開発サーバーがコンソールにメッセージをいくつか表示します
              3. 電話が接続されるとメッセージが聴こえてきます

              And there you go - outbound and inbound voice calling with PHP.

              It worked, I heard it! What's next?

              関連トピック

              Now you know the basics of making and responding to phone calls with PHP.

              This PHP app only used the <Say> TwiML verb to read a message to the caller using text to speech. With different TwiML verbs, you can create other powerful constructs and call flows. Try a few, such as <Record>, <Gather>, and <Conference>.

              以下のページをご覧いただき、さらに理解を深めてください:

              皆さまが何を開発されるのか、目にするのが待ちきれません!

              Rate this page:

              ヘルプが必要ですか?

              We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.