loduis / alegra-php
Alegra PHP 库
v0.25.1
2017-10-21 13:33 UTC
Requires
- php: >=5.6.11
- guzzlehttp/guzzle: ^6.2
- illuminate/support: ^5.4
- loduis/illuminate-api: 1.0.*
- nesbot/carbon: ~1.21
- psy/psysh: @stable
Requires (Dev)
- fzaninotto/faker: ^1.6
- mockery/mockery: ^0.9.4
- phpunit/phpunit: 5.5.*
- vlucas/phpdotenv: ^2.2
This package is auto-updated.
Last update: 2024-09-27 03:17:00 UTC
README
您可以在 http://www.alegra.com/ 上注册 Alegra 账户。
要求
PHP 5.6.11 及以上版本。
Composer
您可以通过 Composer 安装绑定。运行以下命令
composer require "loduis/alegra-php:@dev"
要使用绑定,请使用 Composer 的 自动加载
require_once('vendor/autoload.php');
依赖
绑定需要以下扩展才能正常工作
如果您使用 Composer,这些依赖项应自动处理。如果您手动安装,请确保这些扩展可用。
入门
任何资源都包含五个主要方法(all、get、create、save、delete)
简单用法如下
您的 composer.json 文件
{ "minimum-stability": "dev", "prefer-stable": true, "require": { "loduis/alegra-php": "1.0.*" } }
您的 test.php 脚本
<?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' ]); */
创建 新资源
$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@server.com'; $contact->save(); print_r($contact);
保存 资源而无需获取数据
$contact = new Alegra\Contact(100); $contact->email = 'user@server.com'; $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 }
文档
请参阅 http://developer.alegra.com/docs 以获取最新的文档。