-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.php
56 lines (46 loc) · 1.49 KB
/
app.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
use Telnyx\AvailablePhoneNumber;
require __DIR__ . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$TELNYX_API_KEY = $_ENV["TELNYX_API_KEY"];
Telnyx\Telnyx::setApiKey($TELNYX_API_KEY);
function extractNxx(object $telnyxAvailablePhoneNumberResponse){
$phoneNumber = $telnyxAvailablePhoneNumberResponse['phone_number'];
$npa = substr($phoneNumber, 5, 3);
return $npa;
}
function searchAvailableNumbers(string $areaCode) {
try {
$telnyxResponse = AvailablePhoneNumber::All([
"filter[national_destination_code]" => $areaCode,
"filter[best_effort]" => false,
"filter[limit]" => 100
]);
return $telnyxResponse->data;
} catch (Exception $e){
echo 'Caught exception: ', $e->getMessage(), "\n";
exit;
}
}
function findUniqueCount(array $telnyxResponseData){
$npas = array_map("extractNxx", $telnyxResponseData);
$uniqueValues = array_count_values($npas);
return $uniqueValues;
}
function getAreaCodeFromUser(){
echo "Which NPA (area code) would you like to get NXX counts?: ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
$npa = trim($line);
if(strlen($npa) != 3){
echo "Invalid NPA (area code), NPAs are 3 digits\n";
exit;
}
fclose($handle);
return $npa;
}
$areaCode = getAreaCodeFromUser();
$telnyxResponse = searchAvailableNumbers($areaCode);
$nxxCount = findUniqueCount($telnyxResponse);
print_r($nxxCount);