PHP code example of loduis / alegra-php

1. Go to this page and download the library: Download loduis/alegra-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/ */

    

loduis / alegra-php example snippets






a\Api::auth('Your user', 'Your api token');
// Also you can auth api with your app credentials
/*
Alegra\Api::auth([
    'username' => 'Your user',
    'password' => 'Your password'
]);
*/

$contact = Alegra\Contact::create(['name' => 'Your contact name']);
print_r($contact);

// Create using save

$contact = new Alegra\Contact;
$contact->name = 'My second contact';
$contact->save(); // Update the contact
print_r($contact);

$contact = Alegra\Contact::get(100); // where 100 is the id of resource.
$contact->identification = '900.123.123-8';
$contact->email = '[email protected]';
$contact->save();
print_r($contact);


$contact = new Alegra\Contact(100);
$contact->email = '[email protected]';
$contact->save();

$contacts = Alegra\Contact::all();
$contacts->each(function ($contact) {
    print_r($contact);
});

// $contacts is instanceof Illuminate\Support\Collection
// You can use methods like
print_r($contacts->slice(0, 3)); // The three first contacts.

// Get a delete

Alegra\Contact::get(1)->delete();

// Delete without fetch data

(new Alegra\Contact(1))->delete();

// Delete using static interface

Alegra\Contact::delete(1);

try {
    // Your request code
}

// Exception when a client error is encountered (4xx codes)

catch (GuzzleHttp\Exception\ClientException $e) {
    // code
}

// Exception when a server error is encountered (5xx codes)

catch (GuzzleHttp\Exception\ServerException $e) {
    // code
}

// Exception thrown when a connection cannot be established.

catch (GuzzleHttp\Exception\ConnectException $e) {
    // code
}

// Other exceptions

catch (Exception $e) {
    // code
}

bash
composer