user5603796
user5603796

Reputation: 389

Twilio get all purchased numbers on account

I need to use the Twilio API in PHP to get all purchased numbers on my account so that they can be used in a <select> option in a HTML form.

Example:

<select class="form-control">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
</select>

Can someone show a basic example of how to do this?

Upvotes: 2

Views: 1633

Answers (3)

rgbflawed
rgbflawed

Reputation: 2157

I've done this with:

$twilio=new Client(TWILIO_ACC_ID,TWILIO_AUTH_TOKEN);

//get all the phone numbers on account (assuming there won't be more than 1000)
$incoming_phone_numbers=$twilio->incomingPhoneNumbers->read([],1000);

//loop through all numbers
foreach ($incoming_phone_numbers as $number) {
    print("<p>sid: ".$number->sid." phone number: ".$number->phoneNumber."</p>");
}   

Pretty different than what the other two have posted! I tried those and they didn't come close to working. Maybe I have a different version of the API or php library.

Also Twilio's documentation says to use $number->phone_number but that's clearly wrong. It definitely comes back as $number->phoneNumber

Upvotes: 0

Davis
Davis

Reputation: 846

The Twilio API documentation is pretty thorough and provides examples of how to make most of their calls. They also provide an SDK library to make it even easier. I believe the call you're looking for is /2010-04-01/Accounts/{AccountSid}/IncomingPhoneNumbers (Twilio Documentation). There is an example of calling it at that page:

<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACdc5f132a3c49700934481addd5ce1659"; 
$token = "{{ auth_token }}"; 
$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;
}

You will need to get the "twilio-php" library, which can also be found on their site, or here.

Upvotes: 3

philnash
philnash

Reputation: 73100

Twilio developer evangelist here.

You can get the list of numbers on your account by calling the Incoming Phone Numbers list resource. This is done in PHP, using the Twilio PHP helper library, like this:

<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "{{ account_sid }}"; 
$token = "{{ auth_token }}"; 
$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;
}

I'll leave it to you to turn that into the HTML output you need.

Let me know if this helps.

Upvotes: 0

Related Questions