smsPricing
on your local machine where the code will be housed.$ 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
TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXX TWILIO_AUTH_TOKEN=your_auth_token
index.php
file to 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 = $_ENV['TWILIO_ACCOUNT_SID']; $token = $_ENV['TWILIO_AUTH_TOKEN']; $twilio = new Client($sid, $token);
index.php
, 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"> <!-- Semantic UI CSS --> <link rel="stylesheet" type="text/css" href="/semantic/semantic.min.css"> <title>PHP + Twilio Messaging Pricing API</title> <style> .container { padding: 50px; } p { font-size: 30px; font-weight: 700; text-align: center } .row { padding-top: 50px } </style> </head> <body> <div class="container"> <div class="ui right aligned grid"> <div class="left floated left aligned six wide column"> <div class="center ui segment"> <form class="ui form" method="POST"> <h4 class="ui dividing header">Messaging Pricing Information</h4> <div class="field"> <label>Country</label> <select class="ui fluid dropdown" name="country" required> <option>Country</option> <?php foreach($messagingCountries as $c) { ?> <option value="<?php echo $c->isoCountry; ?>"><?php echo $c->country ?></option> <?php } ?> </select> </div> <input class="ui secondary button" type="submit" name="messaging_price" tabindex="0" value="Okay"/> </form> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script> <script src="semantic/semantic.min.js"></script> </body> </html>
/** MESSAGING PRICES */ if(!empty($_POST['messaging_price'])) { try { $country = $_POST['country']; /** Retrieve the prices to send and receive SMS messages to phone numbers in the given country. */ $prices = $twilio->pricing->v1->messaging ->countries($country) ->fetch(); if($prices) { var_dump($prices, true); } else { print 'Retrieving the messaging pricing information failed. Please try again'; } } 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 = $_ENV['TWILIO_ACCOUNT_SID']; $token = $_ENV['TWILIO_AUTH_TOKEN']; $twilio = new Client($sid, $token); /** Retrieve a list of countries where Twilio messaging services are available */ $messagingCountries = $twilio->pricing->messaging->countries->stream(); if(!empty($_POST['messaging_price'])) { try { $country = $_POST['country']; /** Retrieve the prices to send and receive SMS messages to phone numbers in the given country. */ $prices = $twilio->pricing->v1->messaging ->countries($country) ->fetch(); if($prices) { var_dump($prices, true); } else { print 'Retrieving the messaging pricing information failed. Please try again'; } } 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"> <!-- Semantic UI CSS --> <link rel="stylesheet" type="text/css" href="/semantic/semantic.min.css"> <title>PHP + Twilio Messaging Pricing API</title> <style> .container { padding: 50px; } p { font-size: 30px; font-weight: 700; text-align: center } .row { padding-top: 50px } </style> </head> <body> <div class="container"> <div class="ui right aligned grid"> <div class="left floated left aligned six wide column"> <div class="center ui segment"> <form class="ui form" method="POST"> <h4 class="ui dividing header">Messaging Pricing Information</h4> <div class="field"> <label>Country</label> <select class="ui fluid dropdown" name="country" required> <option>Country</option> <?php foreach($messagingCountries as $c) { ?> <option value="<?php echo $c->isoCountry; ?>"><?php echo $c->country ?></option> <?php } ?> </select> </div> <input class="ui secondary button" type="submit" name="messaging_price" tabindex="0" value="Okay"/> </form> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script> <script src="semantic/semantic.min.js"></script> </body> </html>
$ php -S localhost:8000