PHP code example of coyshdigital / beaconcrm-php

1. Go to this page and download the library: Download coyshdigital/beaconcrm-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

coyshdigital / beaconcrm-php example snippets


use CoyshDigital\Beacon\BeaconClient;

$beacon = BeaconClient::make(getenv('BEACON_ACCOUNT_ID'), getenv('BEACON_API_KEY'));

$people = $beacon->entitiesWithSchema('person');

$id = $people->create(
    $people->payload()
        ->set('name:first', 'Alex')
        ->set('name:last', 'Rivera')
        ->set('emails', '[email protected]')
        ->set('c_monthly_gift', 25.5)
)->entityId();

$beacon = BeaconClient::make(getenv('BEACON_ACCOUNT_ID'), getenv('BEACON_API_KEY'));

if (!$beacon->ping()) {
    // Wrong account ID, or the key was revoked or mistyped.
}

foreach ($beacon->entityTypes()->all() as $type) {
    echo $type->key . ' - ' . $type->label . PHP_EOL;

    foreach ($type->mappableFields() as $field) {
        echo '  ' . $field->key . ' (' . $field->rawType . ')' . PHP_EOL;

        if ($field->options()) {
            echo '    options: ' . implode(', ', $field->options()) . PHP_EOL;
        }
    }
}

$people = $beacon->entitiesWithSchema('person');

$response = $people->create(
    $people->payload()
        ->set('name:first', 'Alex')
        ->set('name:last', 'Rivera')
        ->set('emails', '[email protected]')
        ->set('phone_numbers', '+441234567890')
        ->set('c_tier', 'Gold')          // single-select, sent as ["Gold"]
        ->set('organisation', 4812)      // record link, sent as [4812]
        ->set('c_monthly_gift', 25.5)    // currency, sent as {"value": 25.5}
);

$response->entityId();   // 4100
$response->entity();     // the full record Beacon stored
$response->references(); // linked-record data, when populated

$people->payload()
    ->set('emails', ['[email protected]', '[email protected]'])  // first is primary
    ->set('c_channels', ['Email', 'SMS']);

$response = $people->read(1988);

// Skip linked-record data. Much faster for large exports.
$response = $people->read(1988, populate: false);

// Include archived records.
$response = $people->read(1988, archived: true);

$people->update(1988, $people->payload()->set('c_tier', 'Gold'));

$people->upsert('emails', $people->payload()
    ->set('emails', '[email protected]')
    ->set('c_tier', 'Gold'));

use CoyshDigital\Beacon\Exception\InvalidPayloadException;

try {
    $people->upsert('emails', $people->payload()->set('c_tier', 'Gold'));
} catch (InvalidPayloadException $e) {
    // Upsert key "emails" has no value in the person payload...
}

$response = $people->list(page: 1, perPage: 100, populate: false);

$response->total();     // 39713, the whole match count rather than the page
$response->entities();  // the records, unwrapped

foreach ($people->each(perPage: 200, populate: false) as $person) {
    echo $person['id'];
}

$organisations = $beacon->entitiesWithSchema('organization');

$organisations->link($orgId, 'c_church_admins', $personId);    // keeps the others
$organisations->unlink($orgId, 'c_church_admins', $personId);  // removes just this one
$organisations->links($orgId, 'c_church_admins');              // [4812, 5104]
$organisations->setLinks($orgId, 'c_church_admins', [4812]);   // replaces, deliberately

$field = $beacon->entityTypes()->get('person')?->field('c_home_church');

$field->linksTo();     // ['organization']
$field->linksToIds();  // [41207]

// One request. Matches on the field, and creates the record if nothing matches.
$orgId = $organisations->resolveId('name', $organisations->payload()->set('name', $church));

// Pages the whole record type and compares client-side. Never creates.
$org = $organisations->findBy('name', $church);

$exportId = $beacon->exports()->trigger($templateId)->entity()['id'] ?? null;

$status = $beacon->exports()->status($exportId)->results()[0] ?? [];

$status['status'];   // in_progress or finished
$status['progress']; // 0 to 100

$organisations->create(
    $organisations->payload()->set('address', [[
        'address_line_one' => '12 Example Street',
        'city'             => 'Warwick',
        'postal_code'      => 'CV34 4AB',
        'country_code'     => 'GB',
    ]])
);

$people->payload()
    ->set('address:address_line_one', '12 Example Street')
    ->set('address:city', 'Warwick')
    ->set('address:postal_code', 'CV34 4AB');

use CoyshDigital\Beacon\Exception\ApiException;

try {
    $people->create($payload);
} catch (ApiException $e) {
    $e->getStatus();     // 500
    $e->getErrorCode();  // server_error
    $e->getMessage();    // Oh shoot! An unknown error occurred.
    $e->getRaw();        // Validation error: "gender": 0 Invalid option.
                         // Allowed options: Male, Female, Non-binary, ...
    $e->getPayload();    // what was sent, credentials redacted
    $e->getSummary();    // all of the above on one line, for a log
}

use CoyshDigital\Beacon\Http\RetryPolicy;

$beacon = BeaconClient::make($accountId, $apiKey, new RetryPolicy(
    maxAttempts: 5,
    baseDelayMs: 1000,
));

// Or turn retries off and handle them yourself.
$beacon = BeaconClient::make($accountId, $apiKey, RetryPolicy::none());

$request = $people->createRequest($payload);

$request->method; // POST
$request->path;   // entity/person
$request->uri();  // entity/person?populate=false
$request->body;   // the shaped entity

$response = $beacon->request('GET', 'some_other_endpoint', ['page' => 2]);
$response->toArray();
bash
composer