6
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RESTで、Twilioの電話番号管理をより便利に!

Last updated at Posted at 2014-09-17

REST API経由で電話番号の管理を自由自在に、検索はもちろん購入、更新、削除も可能です。
新しい電話番号の検索や購入はもちろんのこと、契約中の電話番号の設定変更、削除、リストの出力など、電話番号に関する情報を管理できます。
今回はREST API経由で電話番号を管理する方法を紹介します!

本ブログではTwilioのPHPライブラリーを使います。
PHPライブラリーについては下のブログをご覧ください。
Twilioのアプリケーションをより柔軟に、より手軽に、Twilio Helper ライブラリ。

TwiML(Twilio マークアップ言語)についてはこちら!
TwiMLTM: Twilio マークアップ言語

購入可能な電話番号検索

8行目に国コード「JP(日本)」を設定し、9行目に検索する電話番号の一部を設定します。 12行目の「available_phone_nubers」より検索結果が検索され、今現在有効な電話番号が出力されます。
search_tel_no.php
<?php
    require_once('../Services/Twilio.php'); // Loads the library
 
    $sid = "ACxxxxxxxxxxxxxxxxxxx";
    $token = "yyyyyyyyyyyyyyyyyy";
    $client = new Services_Twilio($sid, $token);
 
    $numbers = $client->account->available_phone_numbers->getList('JP', 'Local', array(
        "Contains" => "+8150*******"
    ));
 
    foreach($numbers->available_phone_numbers as $number) {
        echo $number->phone_number . "\n";
    }
?>

電話番号の購入

購入する際にはこちらの情報を設定できます。
・FriendlyNama:電話番号のテキスト識別文字列
・VoiceUrl:電話番号のTwiML URLを設定します。(Request URL)
・PhoneNumber:購入する電話番号を設定します。
・VoiceMethod:POSTもしくはGETを設定します。

このように電話番号の基本情報を設定し、購入することが可能です。

buy_tel_number.php
<?php
    require_once('../Services/Twilio.php'); // Loads the library
 
    $sid = "ACxxxxxxxxxxxxxxx";
    $token = "yyyyyyyyyyyyyyy";
    $client = new Services_Twilio($sid, $token);
 
    $number = $client->account->incoming_phone_numbers->create(array(
        "FriendlyName" => "REST電話番号購入テスト",
        "VoiceUrl" => "http://xxx.xxx.xxx.xxx/twiml.xml",
        "PhoneNumber" => "+8150xxxxyyyy",
        "VoiceMethod" => "GET"
    ));
?>

契約中の電話番号リストを検索

契約中(利用可能)の電話番号を検索してみます。 9行目の「incoming_phone_numbers」よりアカウントに設定されている有効な電話番号が検索されます。 10行目のphone_numberから電話番号が、11行目のsidからphone number sidが出力されます。

※ phone number sid
phone number sidは電話番号を管理する固有のsidを意味します。
この後も出ますが、このsidを利用してより細かな電話番号の操作が可能になります。

incoming_tel_number.php
<?php
    require_once('../Services/Twilio.php'); // Loads the library

    $sid = "ACxxxxxxxxxxxxx";
    $token = "yyyyyyyyyyyyyyy";
    $client = new Services_Twilio($sid, $token);
 
    // Loop over the list of numbers and echo a property for each one
    foreach ($client->account->incoming_phone_numbers as $number) {
        echo $number->phone_number . "\n";
        echo $number->sid . "\n";
    }
?>

電話番号の変更と削除

電話番号設定情報を更新してみたます。 まず、電話番号リストを出して、その中更新対象の電話番号に 12行目の updateから番号の設定情報を更新できます。 VoiceUrl(Request URL)はもちろん、フレンドリーネーム、メソッドなど、電話番号を構成している設定情報は全て更新可能です。
incoming_tel_number_update.php
<?php
    require_once('../Services/Twilio.php'); // Loads the library
 
    // Your Account Sid and Auth Token from twilio.com/user/account
    $sid = "ACxxxxxxxxxxxx";
    $token = "yyyyyyyyyyyy";
    $client = new Services_Twilio($sid, $token);

    foreach ($client->account->incoming_phone_numbers as $number) {
        if ( $number->phone_number == "+815031717476") {
            $number = $client->account->incoming_phone_numbers->get($number->sid);
            $number->update(array(
                "VoiceUrl" => "http://xxx.xxx.xxx.xxx/twiml.xml",
                "FriendlyName" => "フレンドリーネームが変更されます。"
            ));
        }
    }
?>

削除は、11行目の deleteから削除可能です。
ここでも、電話番号の sidを使って番号を削除しています。

incoming_tel_number_delete.php
<?php
    require_once('../Services/Twilio.php'); // Loads the library

    $sid = "ACxxxxxxxxxxxxxxxx";
    $token = "yyyyyyyyyyyy";
    $client = new Services_Twilio($sid, $token);

    foreach ($client->account->incoming_phone_numbers as $number) {
        if ( $number->phone_number == "+815031717476") {
            echo "電話番号:" . $number->phone_number . "を削除します。\n";
            $client->account->incoming_phone_numbers->delete($number->sid);
        }
    }
?>

最後に

このようにREST APIを経由しますと、ダッシュボードにログインする必要もなく、みなさん各自の電話番号管理システムの開発が可能になります。

電話番号の購入は、購入された時点で課金が発生しますので、ご注意ください。

6
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?