PHP クイックスタート:API を介したテキストメッセージの送信

SMS、MMSを使って友達をパーティーに誘ってみよう!

SMSを送信するためにメッセージリソースURIにHTTP POSTを送ります。この機能を使うにはTwilio PHPライブラリを使ってRESTリクエストを作成します。

本例でTwilioのトライアル・アカウントをご使用の場合、Twilioに検証済みの電話番号にのみSMSを送信できます。 電話番号はTwilioコンソール内の検証済み発信者番号にて検証できます。 発信者番号 (from) パラメーターも、Twilioから購入済みの電話番号にする必要があります。  

SDK Version:
  • 4.x
  • 5.x
<?php
    /* Send an SMS using Twilio. You can run this file 3 different ways:
     *
     * 1. Save it as sendnotifications.php and at the command line, run 
     *        php sendnotifications.php
     *
     * 2. Upload it to a web host and load mywebhost.com/sendnotifications.php 
     *    in a web browser.
     *
     * 3. Download a local server like WAMP, MAMP or XAMPP. Point the web root 
     *    directory to the folder containing this file, and load 
     *    localhost:8888/sendnotifications.php in a web browser.
     */

    // Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
    // and move it into the folder containing this sendnotifications.php file.
    require "Services/Twilio.php";

    // Step 2: set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $AuthToken = "your_auth_token";

    // Step 3: instantiate a new Twilio Rest Client
    $client = new Services_Twilio($AccountSid, $AuthToken);

    // Step 4: make an array of people we know, to send them a message. 
    // Feel free to change/add your own phone number and name here.
    $people = array(
        "+15558675309" => "Curious George",
        "+15558675308" => "Boots",
        "+15558675307" => "Virgil",
    );

    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->sendMessage(

            // Step 6: Change the 'From' number below to be a valid Twilio number 
            // that you've purchased
            "+15017250604", 

            // the number we are sending to - Any phone number
            $number,

            // the sms body
            "Hey $name, Monkey Party at 6PM. Bring Bananas!"
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
<?php
    /* Send an SMS using Twilio. You can run this file 3 different ways:
     *
     * 1. Save it as sendnotifications.php and at the command line, run 
     *         php sendnotifications.php
     *
     * 2. Upload it to a web host and load mywebhost.com/sendnotifications.php 
     *    in a web browser.
     *
     * 3. Download a local server like WAMP, MAMP or XAMPP. Point the web root 
     *    directory to the folder containing this file, and load 
     *    localhost:8888/sendnotifications.php in a web browser.
     */

    // Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
    // following the instructions to install it with Composer.
    require_once "vendor/autoload.php";
    use Twilio\Rest\Client;
    
    // Step 2: set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $AuthToken = "your_auth_token";

    // Step 3: instantiate a new Twilio Rest Client
    $client = new Client($AccountSid, $AuthToken);

    // Step 4: make an array of people we know, to send them a message. 
    // Feel free to change/add your own phone number and name here.
    $people = array(
        "+15558675309" => "Curious George",
        "+15558675308" => "Boots",
        "+15558675307" => "Virgil"
    );

    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(
                // Step 6: Change the 'From' number below to be a valid Twilio number 
                // that you've purchased
                'from' => "+15017250604", 
                
                // the sms body
                'body' => "Hey $name, Monkey Party at 6PM. Bring Bananas!"
            )
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
Twilioを使ってSMSを送信する

詳しく見てみましょう。

  • まず、TwilioのWebサイトにアクセスし、Twilioコンソールにログインします。 ダッシュボードには、「アカウントサマリー」と書かれたセクションがあります。 ここにあなたのアカウントSIDと認証トークンが表示されています。 これらの値をコピーし、アカウントSIDと認証トークンの変数にペーストします。

  • 次に、新しいクライアント オブジェクトのインスタンスを生成し、リクエスト メソッドを POST に設定し、To、From、Body パラメーターを連想配列に入れ、Twilio への REST API リクエストを生成します。From パラメーターは、トライアル アカウントの場合は Sandbox 電話番号、アップグレード アカウントの場合は Twilio から購入した電話番号とする必要があります。

REST リクエストが成功すると、SMS は送信キューに入ります。SMS は From 番号当たり最大 1 秒につき 1 件のペースで送信されます。

デフォルトでは、SMS が実際に送信された時、または送信が失敗した時、アプリケーションは通知を受け取りません。SMS が送信された、または送信失敗が起こった時にコールバックを受け取るには、StatusCallback パラメーターを REST リクエ ストに含めます。

REST API で MMS メッセージを送信する

MMSの送信もTwilioを使うと簡単です。MMS を送信するには、HTTP POST リクエストを Message リソースに画像のURLと共に発行します。

MMS(日本未対応)メッセージはMMS対応の番号でのみ送受信可能です。 電話番号の機能対応状況をコンソール内または Available Phone Numbers リソースへの照会によって確認し、SMS対応のTwilio番号を検索できます。

foreach loop を下記のように変更します

SDK Version:
  • 4.x
  • 5.x
<?php
    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->sendMessage(

            // Step 6: Change the 'From' number below to be a valid Twilio number
            // that you've purchased
            "YYY-YYY-YYYY",

            // the number we are sending to - Any phone number
            $number,

            // the sms body
            "Hey $name, Monkey Party at 6PM. Bring Bananas!",

            // Step 7: Add a url to the image media you want to send
            array("https://demo.twilio.com/owl.png", "https://demo.twilio.com/logo.png")
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
<?php
    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(
                // Step 6: Change the 'From' number below to be a valid Twilio number 
                // that you've purchased
                'from' => "+15017250604", 

                // the sms body
                'body' => "Hey $name, Monkey Party at 6PM. Bring Bananas!",
                
                // Step 7: Add url(s) to the image media you want to send
                'mediaUrl' => array("https://demo.twilio.com/owl.png", 
                    "https://demo.twilio.com/logo.png")
            )
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
Twilioを使ってMMSを送信する

SMS, MMS メッセージの送信についての詳細は、REST リソースのドキュメントを参照してください。


次: SMSおよびMMS(日本未対応)メッセージに返信する

1 / 1
Loading Code Samples...
SDK Version:
  • 4.x
  • 5.x
<?php
    /* Send an SMS using Twilio. You can run this file 3 different ways:
     *
     * 1. Save it as sendnotifications.php and at the command line, run 
     *        php sendnotifications.php
     *
     * 2. Upload it to a web host and load mywebhost.com/sendnotifications.php 
     *    in a web browser.
     *
     * 3. Download a local server like WAMP, MAMP or XAMPP. Point the web root 
     *    directory to the folder containing this file, and load 
     *    localhost:8888/sendnotifications.php in a web browser.
     */

    // Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
    // and move it into the folder containing this sendnotifications.php file.
    require "Services/Twilio.php";

    // Step 2: set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $AuthToken = "your_auth_token";

    // Step 3: instantiate a new Twilio Rest Client
    $client = new Services_Twilio($AccountSid, $AuthToken);

    // Step 4: make an array of people we know, to send them a message. 
    // Feel free to change/add your own phone number and name here.
    $people = array(
        "+15558675309" => "Curious George",
        "+15558675308" => "Boots",
        "+15558675307" => "Virgil",
    );

    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->sendMessage(

            // Step 6: Change the 'From' number below to be a valid Twilio number 
            // that you've purchased
            "+15017250604", 

            // the number we are sending to - Any phone number
            $number,

            // the sms body
            "Hey $name, Monkey Party at 6PM. Bring Bananas!"
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
<?php
    /* Send an SMS using Twilio. You can run this file 3 different ways:
     *
     * 1. Save it as sendnotifications.php and at the command line, run 
     *         php sendnotifications.php
     *
     * 2. Upload it to a web host and load mywebhost.com/sendnotifications.php 
     *    in a web browser.
     *
     * 3. Download a local server like WAMP, MAMP or XAMPP. Point the web root 
     *    directory to the folder containing this file, and load 
     *    localhost:8888/sendnotifications.php in a web browser.
     */

    // Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
    // following the instructions to install it with Composer.
    require_once "vendor/autoload.php";
    use Twilio\Rest\Client;
    
    // Step 2: set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $AuthToken = "your_auth_token";

    // Step 3: instantiate a new Twilio Rest Client
    $client = new Client($AccountSid, $AuthToken);

    // Step 4: make an array of people we know, to send them a message. 
    // Feel free to change/add your own phone number and name here.
    $people = array(
        "+15558675309" => "Curious George",
        "+15558675308" => "Boots",
        "+15558675307" => "Virgil"
    );

    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(
                // Step 6: Change the 'From' number below to be a valid Twilio number 
                // that you've purchased
                'from' => "+15017250604", 
                
                // the sms body
                'body' => "Hey $name, Monkey Party at 6PM. Bring Bananas!"
            )
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
SDK Version:
  • 4.x
  • 5.x
<?php
    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->sendMessage(

            // Step 6: Change the 'From' number below to be a valid Twilio number
            // that you've purchased
            "YYY-YYY-YYYY",

            // the number we are sending to - Any phone number
            $number,

            // the sms body
            "Hey $name, Monkey Party at 6PM. Bring Bananas!",

            // Step 7: Add a url to the image media you want to send
            array("https://demo.twilio.com/owl.png", "https://demo.twilio.com/logo.png")
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }
<?php
    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(
                // Step 6: Change the 'From' number below to be a valid Twilio number 
                // that you've purchased
                'from' => "+15017250604", 

                // the sms body
                'body' => "Hey $name, Monkey Party at 6PM. Bring Bananas!",
                
                // Step 7: Add url(s) to the image media you want to send
                'mediaUrl' => array("https://demo.twilio.com/owl.png", 
                    "https://demo.twilio.com/logo.png")
            )
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";
    }