pricing
on your local machine where the code will be housed.index.php
file which will contain all the code for this tutorial.$ composer require twilio/sdk
.env
file to store your environment variables. The .env
file will be read using the popular PHP package vlucas/phpdotenv to store those variables.$ composer require vlucas/phpdotenv
.env
file, as they will be used for authenticating the email requests. Add the following block to the file:TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXX TWILIO_AUTH_TOKEN=your_auth_token TWILIO_PHONE=your_twilio_phone_number
index.php
file and include the Composer autoloader, and the Twilio PHP SDK like so:<?php // Required if your environment does not handle autoloading require __DIR__ . '/vendor/autoload.php'; use Twilio\Rest\Client; $dotenv = Dotenv\Dotenv::createImmutable(__DIR__, '/.env'); $dotenv->load();
// Your Account SID and Auth Token from twilio.com/console $sid = getenv('TWILIO_ACCOUNT_SID'); $token = getenv('TWILIO_AUTH_TOKEN'); $twilioNumber = getenv('TWILIO_PHONE'); $twilio = new Client($sid, $token);
index.php
. Add the following code to the created view, below the PHP code:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <title>PHP + Twilio Pricing API</title> <style> .container { padding-top: 50px; } p { font-size: 30px; font-weight: 700; text-align: center } .row { padding-top: 50px } </style> </head> <body> <div class="container"> <p>PROGRAMMABLE VOICE</p> <div class="row"> <div class="col-6"> <div class="card"> <h5 class="card-header">Fetch Number</h5> <div class="card-body"> <form method="POST"> <div class="form-group"> <label for="phone_number">Phone number</label> <input type="text" class="form-control" name="phone_number" aria-describedby="numberHelp" required> <small id="numberHelp" class="form-text text-muted">Destination phone number.</small> </div> <input type="submit" name="fetch_number" class="btn btn-primary" value="Submit"/> </form> </div> </div> </div> <div class="col-6"> <div class="card"> <h5 class="card-header">Voice prices by origination number</h5> <div class="card-body"> <form method="POST"> <div class="form-group"> <label for="destination_number">Destination number</label> <input type="text" class="form-control" name="destination_number" aria-describedby="numberHelp"> <small id="numberHelp" class="form-text text-muted">Destination phone number.</small> </div> <input type="submit" name="price_by_origination" class="btn btn-primary" value="Submit"/> </form> </div> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> </body> </html>
/** PROGRAMMABLE VOICE */ if (!empty($_POST['fetch_number'])) { try { $phone = $_POST['phone_number']; /** Lookup the phone number to make sure it is valid before retrieving the pricing information */ $validity = $twilio->lookups->v1->phoneNumbers($phone)->fetch(); if($validity) { /** If the phone number is valid, proceed to retrieve the pricing information */ $number = $twilio->pricing->v2->voice->numbers($phone)->fetch(); if($number) { var_dump($number); } else { echo 'Retrieving the information failed. Please try again'; } } else { echo 'Invalid phone number. Please check it and retry'; } } catch (Exception $e) { print 'Error: ' . $e->getMessage(); } }
/** PROGRAMMABLE VOICE */ if(!empty($_POST['price_by_origination'])) { try { $destinationNumber = $_POST['destination_number']; /** Lookup the phone number to make sure it is valid before retrieving the pricing information */ $validity = $twilio->lookups->v1->phoneNumbers($destinationNumber)->fetch(); if($validity) { /** * Call the API to retrieve the information for calls to the destination number. */ $number = $twilio->pricing->v2->voice ->numbers($destinationNumber) ->fetch(['originationNumber' => $twilioNumber]); if($number) { var_dump($number); } else { print 'Retrieving the information failed. Please try again'; } } else { echo 'Invalid phone number. Please check it and retry'; } } catch (Exception $e) { print 'Error: ' . $e->getMessage(); } }
index.php
file with the code below:<?php // Required if your environment does not handle autoloading require __DIR__ . '/vendor/autoload.php'; use Twilio\Rest\Client; $dotenv = Dotenv\Dotenv::createImmutable(__DIR__, '/.env'); $dotenv->load(); // Your Account SID and Auth Token from twilio.com/console $sid = getenv('TWILIO_ACCOUNT_SID'); $token = getenv('TWILIO_AUTH_TOKEN'); $twilioNumber = getenv('TWILIO_PHONE'); $twilio = new Client($sid, $token); /** Retrieve a list of countries where Twilio phone number services are available */ $phoneCountries = $twilio->pricing->phoneNumbers->countries->read(); if (!empty($_POST['fetch_number'])) { try { $phone = $_POST['phone_number']; /** Lookup the phone number to make sure it is valid before retrieving the pricing information */ $validity = $twilio->lookups->v1->phoneNumbers($phone)->fetch(); if($validity) { /** If the phone number is valid, proceed to retrieve the pricing information */ $number = $twilio->pricing->v2->voice->numbers($phone)->fetch(); if($number) { var_dump($number); } else { echo 'Retrieving the information failed. Please try again'; } } else { echo 'Invalid phone number. Please check it and retry'; } } catch (Exception $e) { print 'Error: ' . $e->getMessage(); } } if(!empty($_POST['price_by_origination'])) { try { $destinationNumber = $_POST['destination_number']; /** Lookup the phone number to make sure it is valid before retrieving the pricing information */ $validity = $twilio->lookups->v1->phoneNumbers($destinationNumber)->fetch(); if($validity) { /** * Call the API to retrieve the information for calls to the destination number. */ $number = $twilio->pricing->v2->voice ->numbers($destinationNumber) ->fetch(['originationNumber' => $twilioNumber]); if($number) { var_dump($number); } else { print 'Retrieving the information failed. Please try again'; } } else { echo 'Invalid phone number. Please check it and retry'; } } catch (Exception $e) { print 'Error: ' . $e->getMessage(); } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <title>PHP + Twilio Pricing API</title> <style> .container { padding-top: 50px; } p { font-size: 30px; font-weight: 700; text-align: center } .row { padding-top: 50px } </style> </head> <body> <div class="container"> <p>PROGRAMMABLE VOICE</p> <div class="row"> <div class="col-6"> <div class="card"> <h5 class="card-header">Fetch Number</h5> <div class="card-body"> <form method="POST"> <div class="form-group"> <label for="phone_number">Phone number</label> <input type="text" class="form-control" name="phone_number" aria-describedby="numberHelp" required> <small id="numberHelp" class="form-text text-muted">Destination phone number.</small> </div> <input type="submit" name="fetch_number" class="btn btn-primary" value="Submit"/> </form> </div> </div> </div> <div class="col-6"> <div class="card"> <h5 class="card-header">Voice prices by origination number</h5> <div class="card-body"> <form method="POST"> <div class="form-group"> <label for="destination_number">Destination number</label> <input type="text" class="form-control" name="destination_number" aria-describedby="numberHelp"> <small id="numberHelp" class="form-text text-muted">Destination phone number.</small> </div> <input type="submit" name="price_by_origination" class="btn btn-primary" value="Submit"/> </form> </div> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> </body> </html>
$ php -S localhost:8000