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

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.

主站蜘蛛池模板: 汝南县| 舟山市| 广昌县| 永兴县| 新沂市| 武山县| 建阳市| 鹿邑县| 咸丰县| 西丰县| 玉树县| 老河口市| 瑞丽市| 平塘县| 双牌县| 孙吴县| 吐鲁番市| 家居| 栾城县| 黔西县| 伊川县| 西宁市| 德格县| 苏尼特左旗| 乌兰县| 开原市| 修水县| 二连浩特市| 巨野县| 济南市| 望谟县| 沙坪坝区| 富蕴县| 全椒县| 巴林右旗| 潮安县| 白沙| 确山县| 贵州省| 东阳市| 雷州市|