planningcenter / developers

Planning Center API docs and support
https://developer.planning.center/docs/
85 stars 9 forks source link

Can't get to work #1254

Closed dsuguy2007 closed 1 day ago

dsuguy2007 commented 1 day ago

Related Product Which product is this question related to? People

Describe the question I tried to write some code with help from chatGPT. I can't get it to work. Below is the code. Any help would be appreciated. I have checked and there should be like 5 upcoming birthdays and 2 anniversaries but this code just returns "No upcoming..."

code.php `function getUpcomingEvents($appId, $appSecret) { $apiBaseUrl = 'https://api.planningcenteronline.com/people/v2/people'; $today = new DateTime();

$nextWeek = (new DateTime())->modify('+7 days');

$authHeaders = base64_encode("$appId:$appSecret");

// Initialize cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiBaseUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Basic $authHeaders"
]);

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

// Check for errors
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
    return null;
}

// Close cURL session
curl_close($ch);

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

$upcomingBirthdays = [];
$upcomingAnniversaries = [];

// Iterate over each person and determine if their birthday or anniversary falls within the next week
foreach ($data['data'] as $person) {
    if (isset($person['attributes']['birthday'])) {
        $birthday = DateTime::createFromFormat('Y-m-d', $person['attributes']['birthday']);
        if ($today <= $birthday && $birthday <= $nextWeek) {
            $upcomingBirthdays[] = $person['attributes'];
        }
    }

    if (isset($person['attributes']['anniversary'])) {
        $anniversary = DateTime::createFromFormat('Y-m-d', $person['attributes']['anniversary']);
        if ($today <= $anniversary && $anniversary <= $nextWeek) {
            $upcomingAnniversaries[] = $person['attributes'];
        }
    }
}

return [$upcomingBirthdays, $upcomingAnniversaries];

}

// Replace with your actual PCO App ID and Secret $API_APP_ID = 'my_app_ID'; $API_SECRET = 'my API secret';

list($birthdays, $anniversaries) = getUpcomingEvents($API_APP_ID, $API_SECRET); `

index.php `<!doctype html>

Upcoming Birthdays and Anniversaries

Upcoming Birthdays

Upcoming Anniversaries

` ## I have.. - [x] Reviewed the documentation found at https://developer.planning.center/docs - [x] Searched for previous issues answering this question - [x] Removed all private information from this issue (credentials, tokens, emails, phone numbers, etc.) - [x] Reviewed my issue for completeness
seven1m commented 1 day ago

While we cannot help debug your code, my first observation is that your code only fetches the first 25 people in your organization. If you view https://api.planningcenteronline.com/people/v2/people in your browser, you will see that there are links that point to the next page. You will need to fetch that next link and keep fetching until you run out of pages.