<?php


/*
$customernumber = "+1234567898";
$curl = curl_init();
                curl_setopt_array($curl, array(
                  CURLOPT_URL => 'https://services.leadconnectorhq.com/contacts/',
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_ENCODING => '',
                  CURLOPT_MAXREDIRS => 10,
                  CURLOPT_TIMEOUT => 0,
                  CURLOPT_FOLLOWLOCATION => true,
                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                  CURLOPT_CUSTOMREQUEST => 'POST',
                  CURLOPT_POSTFIELDS =>'{

                  "locationId": "Poglt4vXmBwxfTC0oL2z",
                  "phone": "'.$customernumber.'",
                  "source": "public api"
                }',
                  CURLOPT_HTTPHEADER => array(
                    'Accept: application/json',
                    'Authorization: Bearer pit-0fc666b1-0f93-4494-96f7-b24d65e56a73',
                    'Content-Type: application/json',
                    'Version: 2021-07-28'
                  ),
                ));
                
                $response = curl_exec($curl);
                curl_close($curl);
                echo $response;
             
                die("contact created");*/


function getAllContacts() {
    $locationId = 'Poglt4vXmBwxfTC0oL2z';
    $apiKey = 'pit-0fc666b1-0f93-4494-96f7-b24d65e56a73';
    
    $curl = curl_init();
    $apiUrl = "https://services.leadconnectorhq.com/contacts/?locationId=$locationId";
    $headers = [
        'Accept: application/json',
        'Authorization: Bearer ' . $apiKey,
        'Version: 2021-07-28'
    ];
    $allContacts = [];
    $currentPageUrl = $apiUrl;
    
    do {
        // Set the cURL options
        curl_setopt_array($curl, [
            CURLOPT_URL => $currentPageUrl,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_HTTPHEADER => $headers,
        ]);

        // Execute the cURL request and get the response
        $response = curl_exec($curl);

        // Check for cURL errors
        if ($response === false) {
            echo "Error: " . curl_error($curl);
            break;
        }

        // Decode the JSON response
        $data = json_decode($response, true);

        // If the request was successful and contains contacts, add them to the allContacts array
        if (isset($data['contacts'])) {
            $allContacts = array_merge($allContacts, $data['contacts']);
        }

        // Check if there's a next page and update the currentPageUrl
        if (isset($data['meta']['nextPageUrl'])) {
            $currentPageUrl = $data['meta']['nextPageUrl'];
        } else {
            // No more pages, exit the loop
            $currentPageUrl = null;
        }
    } while ($currentPageUrl !== null);

    // Close the cURL session
    curl_close($curl);

    return $allContacts;
}



print_r($contacts); // Print all the contacts for verification
?>
