<?php
require 'vendor/autoload.php'; // Include Guzzle for HTTP requests

use GuzzleHttp\Client;

// Validate and sanitize input
$email = "rthapa@thgenesis.com";
$phone = "+14057618957";


//$name = htmlspecialchars($_POST['name']);
//$phone = htmlspecialchars($_POST['phone']);

// Optionally, store the user data in a database

// Prepare Attentive API request
$client = new Client();
try {
    $response = $client->request('POST', 'https://api.attentivemobile.com/v1/attributes/custom', [
        'headers' => [
            'Authorization' => 'Bearer UlNiUnFRS2hLZUJPRjE3YzBheEhVdWhQNEZXaU9WdTJZMUpD',
            'Accept'        => 'application/json',
            'Content-Type'  => 'application/json'
        ],
        'json' => [
             "properties"=> [
      "age"=> "24",
      "birthday"=> "1986-11-16",
    
      
      
    ],
        'user' => [
            'phone' => $phone
           
        ],
        
      
        'singleOptIn' => false,
        
    'locale'=> [
     'language' => 'en',
     'country' => 'US'
   ],
    ]
    ]);

    $data = json_decode($response->getBody(), true);
    print_r($data);
    // Process success response
    echo "Subscription successful!";
} catch (Exception $e) {
    echo '<pre>';
    print_r($e);
    // Handle errors
    echo 'Subscription failed: ' . $e->getMessage();
}


/*

function signUpForAttentive($firstName, $phoneNumber, $signUpSourceId) {
    $url = 'https://api.attentivemobile.com/v1/subscriptions';
    $authToken = 'Bearer UlNiUnFRS2hLZUJPRjE3YzBheEhVdWhQNEZXaU9WdTJZMUpD'; // Your API token

    $data = [
        'user' => [
            'phone' => $phoneNumber,
        ],
        'signUpSourceId' => $signUpSourceId,
        'subscriptionType' => 'MARKETING',
        'singleOptIn' => true,
        'locale' => [
            'language' => 'en',
            'country' => 'US'
        ]
    ];

    $options = [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($data),
        CURLOPT_HTTPHEADER => [
            "Accept: application/json",
            "Content-Type: application/json",
            "Authorization: " . $authToken
        ],
    ];

    $curl = curl_init();
    curl_setopt_array($curl, $options);
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        return $response;
    }
}

// Usage Example
$response = signUpForAttentive("John Doe", "1234567890", "sourceId123");
echo $response;

*/