<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require __DIR__ . '/vendor/autoload.php';

$apiKey = "KEY0181F908F3E2FE549BC91C964C75FEDF_yxpGve7TsRSaP5Sz4ysTR6";  // Store API Key Securely

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $phone = $_POST["phone"];
    $phone = preg_replace("/[^0-9]/", "", $phone); // Sanitize input

    $client = new GuzzleHttp\Client();
    try {
        $response = $client->request('GET', "https://api.telnyx.com/v2/number_lookup/{$phone}?type=carrier", [
            'headers' => [
                'Authorization' => "Bearer {$apiKey}",
                'Content-Type' => 'application/json',
                'Accept' => 'application/json'
            ]
        ]);

        echo $response->getBody();

    } catch (Exception $e) {
        http_response_code(500);
        echo json_encode(['error' => $e->getMessage()]);
    }
} else {
    http_response_code(405);
    echo json_encode(['error' => 'Invalid request method']);
}
?>
