You can sign up for a Alegra account at http://www.alegra.com/.
PHP 5.6.11 and later.
- Include in your project
composer.json
the following dependency:
"require": {
...
"jorgadan/alegra-php": "^0.27"
},
- Add a repository key to your project
composer.json
:
"repositories": [
{
"type": "git",
"url": "[email protected]:jorgadan/alegra-php.git"
}
],
- Run
composer update
orcomposer install
and you should be getting it
To use the bindings, use Composer's autoload:
require_once('vendor/autoload.php');
The bindings require the following extension in order to work properly:
If you use Composer, these dependencies should be handled automatically. If you install manually, you'll want to make sure that these extensions are available.
Your test.php script
<?php
require './vendor/autoload.php';
Alegra\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'
]);
*/
Create an new resource
$contact = Alegra\Contact::create(['name' => 'Your contact name']);
print_r($contact);
// Create using save
$contact = new Alegra\Contact;
$contact->name = 'My second contact';
//a LEGAL_ENTITY will be created by default
//to create a PERSON_ENTITY you must add
//$contact->firstName = "My";
//$contact->secondName = "Second";
//$contact->lastName = "Contact";
$contact->save(); // Update the contact
print_r($contact);
Get an existing resource
$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);
Save an resource without fetch your data
$contact = new Alegra\Contact(100);
$contact->email = '[email protected]';
$contact->save();
Get all resources
$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.
Delete an resource
// 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);
Catch errors
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
}
Please see http://developer.alegra.com/docs for up-to-date documentation.