Speed up development with full-stack environments for every branch.

Learn More

How to search for available Twilio phone numbers [PHP and telephony]

20 Runs 481 Views 155 Copies
Saved

Saved

dipesh.parmar 55

dipesh.parmar
published 4 years ago

<div style="margin-left:50px;">
<?php
  require('twilio-php-master/Services/Twilio.php');
	
	$AccountSid = getenv("SID"); // Your Account SID from www.twilio.com/user/account
	$AuthToken  = getenv("TOKEN"); // Your Auth Token from www.twilio.com/user/account
	
	$client = new Services_Twilio($AccountSid, $AuthToken);
	
	/* Buy the selected Twilio Number */
	if(!empty($_POST['submit']) && $_POST['submit'] == 'BUY')
	{
		$PhoneNumber = $_POST['PhoneNumber'];
		try
		{
			/* Purchase the selected PhoneNumber */
			$number = $client->account->incoming_phone_numbers->create(array(
				'PhoneNumber' => $PhoneNumber
			));
		}
		catch (Exception $e)
		{
			$err = urlencode("Error purchasing number:". $e->getMessage());
			header("Location: ?msg=$err");
			exit(0);
		}
		$msg = urlencode("Thank you for purchasing $PhoneNumber");
		header("Location: ?msg=$msg");
		exit(0);
	}
	
	/* Handle Searches from the Twilio number search form */
	if(!empty($_POST['submit']) && $_POST['submit'] == 'SEARCH'):
		$SearchParams = array();
		/* Search parameters for US Local PhoneNumbers */
		$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
		{
			/* Initiate US Local PhoneNumber search with $SearchParams list */
			$numbers = $client->account->available_phone_numbers->getList('US', 'Local', $SearchParams);
			/* If we did not find any phone numbers let the user know */
			if(empty($numbers))
			{
				$err = urlencode("We didn't find any phone numbers by that search");
				header("Location: ?msg=$err");
				exit(0);
			}
		}
		catch (Exception $e)
		{
			$err = urlencode("Error processing search:". $e->getMessage());
			header("Location: ?msg=$err");
			exit(0);
		}
	?>
	<html>
		<head>
			<title>Choose a Twilio number to buy</title>
		</head>
		<body>
			<h3>Choose a Twilio number to buy</h3>
			<?php foreach($numbers->available_phone_numbers as $number): ?>
				<form method="POST">
					<label><?php echo $number->friendly_name ?></label>
					<input type="hidden" name="PhoneNumber" value="<?php echo $number->phone_number ?>">
					<input type="submit" name="submit" value="BUY" />
				</form>
			<?php endforeach; ?>
		</body>
	</html>
	<?php endif; ?>
	
	
	
<?php if(empty($_POST['submit'])): /* Display Form for searching AvailablePhoneNumbers */?>
<html>
	<head>
		<title>Find a Twilio number to buy</title>
	</head>
	<body>
		<h3>Find a Twilio number to buy</h3>
		<?php if(!empty($_GET['msg'])): ?>
			<p class="msg" style="border:1px solid red;padding:5px;background-color:tomato;color:white;"><?php echo htmlspecialchars($_GET['msg']); ?></p>
		<?php endif;?>
		<form method="POST">
			<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***EPIC): </label><input type="text" size="7" name="contains"/><br/>
			<input type="submit" name="submit" value="SEARCH"/>
		</form>
	</body>
</html>
<?php endif; ?>
</div>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).