<?php
require __DIR__ . '/vendor/autoload.php'; // Important for Guzzle

$apiKey = "KEY0181F908F3E2FE549BC91C964C75FEDF_yxpGve7TsRSaP5Sz4ysTR6"; // Your API key - ONLY HERE

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $phone = $_POST["phone"];

    // Sanitize input VERY IMPORTANT!
    $phone = preg_replace("/[^0-9]/", "", $phone); // Keep only digits


    $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'
            ]
        ]);

        $data = json_decode($response->getBody(), true);
        echo json_encode($data);

    } catch (Exception $e) {
        http_response_code(500);
        echo json_encode(['error' => $e->getMessage()]); // Send error message (log detailed errors on server)
    }
} else {
    http_response_code(405); // Method Not Allowed
    echo json_encode(['error' => 'Invalid request method']);
}
?>