Buying a phone number

Buying a phone number is an integral part of the Twilio system. If you have multiple users, you can assign each user their own phone number.

Twilio gives you options to pass on numbers to your users, so you can actually search for phone numbers.

You can search by postal code; patterns, such as STRINGER; or for phone numbers near your location.

I've found this ability handy for systems with multiple users or for setting up business numbers for various purposes, such as sales, support, or unique phone numbers for campaigns that are being run at the time.

Getting ready

The complete source code for this recipe can be found in the Chapter2/Recipe3 folder.

How to do it...

Are you ready to learn how to buy a phone number? This recipe will take you step by step through the process.

  1. Download the Twilio Helper Library from https://github.com/twilio/twilio-php/zipball/master and unzip it.
  2. Upload the Services/ folder to your website.
  3. Upload config.php to your website and make sure the following variables are set:
    <?php
      $accountsid = '';  //  YOUR TWILIO ACCOUNT SID
      $authtoken = '';  //    YOUR TWILIO AUTH TOKEN
      $fromNumber = '';  //  PHONE NUMBER CALLS WILL COME FROM
    ?>
  4. Create a file on your website called buy-phone-number.php, with the following code:
    <?php
      include 'Services/Twilio.php';
      include("config.php");
      $client = new Services_Twilio($accountsid, $authtoken);
    ?>
      <h3>Find a number to buy</h3>
      <?php if(!empty($_GET['msg'])): ?>
        <p class="msg"><?php echo htmlspecialchars($_GET['msg']); ?></p>
      <?php endif;?>
      <form method="POST" action="search.php">
      <label>near US postal code (e.g. 94117): </label><input type="text" size="4" name="postal_code"/><br/>
      <label>near this other number (e.g. +14156562345): </label><input type="text" size="7" name="near_number"/><br/>
      <label>matching this pattern (e.g. 415***MINE): </label><input type="text" size="7" name="contains"/><br/>
      <input type="hidden" name="action" value="search" />
      <input type="submit" name="submit" value="SEARCH"/>
      </form>
  5. Create a file on your website called search.php, with the following code:
    <?php
      include 'Services/Twilio.php';
      include("config.php");
      $client = new Services_Twilio($accountsid, $authtoken);
    
      $SearchParams = array();
      $SearchParams['InPostalCode'] = !empty($_POST['postal_code']) ? trim($_POST['postal_code']) : '';
      $SearchParams['NearNumber'] = !empty($_POST['near_number']) ? trim($_POST['near_number']) : '';
      $SearchParams['Contains'] = !empty($_POST['contains'])? trim($_POST['contains']) : '' ;
      try {
        $numbers = $client->account->available_phone_numbers->getList('US', 'Local', $SearchParams);
        if(empty($numbers)) {
          $err = urlencode("We didn't find any phone numbers by that search");
          header("Location: buy-phone-number.php?msg=$err");
          exit(0);
        }
      } catch (Exception $e) {
        $err = urlencode("Error processing search: {$e->getMessage()}");
        header("Location: buy-phone-number.php?msg=$err");
        exit(0);
      }
    ?>
      <h3>Choose a Twilio number to buy</h3>
      <?php foreach($numbers->available_phone_numbers as $number){ ?>
      <form method="POST" action="buy.php">
      <label><?php echo $number->friendly_name ?></label>
      <input type="hidden" name="PhoneNumber" value="<?php echo $number->phone_number ?>">
      <input type="hidden" name="action" value="buy" />
      <input type="submit" name="submit" value="BUY" />
      </form>
      <?php } ?>
  6. Create a file on your website called buy.php, with the following code:
    <?php
      include 'Services/Twilio.php';
      include("config.php");
      $client = new Services_Twilio($accountsid, $authtoken);
    
      $PhoneNumber = $_POST['PhoneNumber'];
      try {
        $number = $client->account->incoming_phone_numbers->create(array(
          'PhoneNumber' => $PhoneNumber
        ));
      } catch (Exception $e) {
        $err = urlencode("Error purchasing number: {$e->getMessage()}");
        header("Location: buy-phone-number.php?msg=$err");
        exit(0);
      }
      $msg = urlencode("Thank you for purchasing $PhoneNumber");
      header("Location: buy-phone-number.php?msg=$msg");
      exit(0);
      break;
    ?>

How it works…

In steps 1 and 2, we downloaded and installed the Twilio Helper Library for PHP. This library is at the heart of your Twilio-powered apps.

In step 3, we uploaded config.php that contains our authentication information to communicate with Twilio's API.

When a user goes to buy-phone-number.php, they are presented with a set of options. He/she can search by the postal code, phone number, or phone patterns.

Once they perform the search, we return a list of phone numbers. The user can then buy any number he/she chooses and that number then belongs to him/her.

Integrate this into your web apps and let your users add their own phone numbers to their accounts.