官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 进贤县| 宝鸡市| 绥德县| 金沙县| 万源市| 改则县| 娄底市| 宝清县| 延庆县| 利川市| 天峻县| 溆浦县| 岳池县| 达拉特旗| 贺州市| 阿城市| 朝阳市| 宜丰县| 资阳市| 宁南县| 武隆县| 米脂县| 靖西县| 溧水县| 海伦市| 高碑店市| 房山区| 东兴市| 惠来县| 景德镇市| 永吉县| 济宁市| 东阿县| 荥阳市| 辽源市| 华亭县| 平利县| 木兰县| 云浮市| 桐庐县| 荔浦县|