Skip to content

guttedgarden/AmoCRMApi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

API client for amoCRM

This package implements an API client with support for basic entities and authorization via the OAuth 2.0 protocol in amoCRM. PHP version no lower than 7.1 is required to work.


Table of contents


Examples

Tokens

JSON file with tokens is stored in the location specified by the user. The JSON file itself is the following structure:

{
  "access_token": ...,
  "refresh_token": ...,
  "token_type": ...,
  "begin_in": ...,
  "expires_in": ...,
}

Creating a new client

If a JSON file with Refresh Token exists, then the following construction is used (If no code is passed, the Refresh method will be called. If passed, the Auth method will be called.)

use App\Client\AmoApiClient;

$client = new AmoApiClient(
    $subDomain,
    $client_id,
    $client_secret,
    $redirect_uri
);

If this is the first run, then the following construction is used

use App\Client\AmoApiClient;

$client = new AmoApiClient(
    $subDomain,
    $client_id,
    $client_secret,
    $redirect_uri,
    $code
);

// OR
$client = new AmoApiClient(
    $subDomain,
    $client_id,
    $client_secret,
    $redirect_uri
);
$client->setAuthCode($code);

Getting token

try {
    $tokens = $client->getToken();
} catch (Exception $exception){
    echo $exception;
}

Leads

Get all leads

try{
    $leads = $client->leads()->getAll([]);
    print_r($leads);
} catch (Exception $exception){
    echo $exception;
}

Get lead by id

try {
    $lead = $client->leads()->create()->getById(28643682);
} catch (Exception $exception){
    echo $exception;
}

Create new lead

try{
    $lead = $client->leads()->create();
    $lead->name = "VTestNew213152224";
    $lead->price = 321;
    print_r($lead->save());
} catch (Exception $exception){
    echo $exception;
}

Update lead

try {
    $lead = $client->leads()->create();
    $lead->id = 28709597;
    $lead->name = "APVTestUpdate32";
    $lead->price = 123;
    print_r($lead->update());
} catch (Exception $exception){
    echo $exception;
}

Add note to lead

try {
    $lead = $client->leads()->create()->getById(28709597);
    $note = $lead->newNote();
    $note->note_type = NoteConstants::NOTE_TYPE_COMMON;
    $note->text = "New test note";
    print_r($lead->addNote($note));
} catch (Exception $exception){
    echo $exception;
}

Contact

Get all contacts

try{
    $contact = $client->contacts()->getAll([]);
    print_r($contact);
}catch (Exception $exception){
    echo $exception;
}

Get contact by id

try {
    $contact = $client->contacts()->create()->getById(46050921);
    print_r($contact->getFieldsAsArray());
} catch (Exception $exception){
    echo $exception;
}

Create new contact

try{
    $contact = $client->contacts()->create();
    $contact->name = "APVTestNewTestContact";
    print_r($contact->save());
} catch (Exception $exception){
    echo $exception;
}

Update contact

try {
    $contact = $client->contacts()->create();
    $contact->id = 46102629;
    $contact->name = "APVTestNewContactUpdate!";
    print_r($contact->update());
} catch (Exception $exception){
    echo $exception;
}

Add note to contact

try {
    $contact = $client->contacts()->create()->getById(46102629);
    $note = $contact->newNote();
    $note->note_type = NoteConstants::NOTE_TYPE_COMMON;
    $note->text = "New test note to ContactModel";
    print_r($contact->addNote($note));
} catch (Exception $exception){
    echo $exception;
}

Company

Get all companys

try{
    $company = $client->company()->getAll([]);
    print_r($company);
}catch (Exception $exception){
    echo $exception;
}

Get company by id

try {
    $company = $client->company()->create()->getById(46050853);
    print_r($company->getFieldsAsArray());
} catch (Exception $exception){
    echo $exception;
}

Create new company

try{
    $company = $client->company()->create();
    $company->name = "APVTestNewCompany";
    print_r($company->save());
} catch (Exception $exception){
    echo $exception;
}

Update company

try {
    $company = $client->company()->create();
    $company->id = 46050853;
    $company->name = "APVTestNewCompanyUpdate";
    print_r($company->update());
} catch (Exception $exception){
    echo $exception;
}

Add note to company

try {
    $company = $client->company()->create()->getById(46102613);
    $note = $company->newNote();
    $note->note_type = NoteConstants::NOTE_TYPE_COMMON;
    $note->text = "New test note to CompanyModel!";
    print_r($company->addNote($note));
} catch (Exception $exception){
    echo $exception;
}

Customer

Get all customers

try{
    $customer = $client->customers();
    print_r($customer->getAll([]));
}catch (Exception $exception){
    echo $exception;
}

Get customer by id

try {
    $customer = $client->customers()->create()->getById(183665);
    print_r($customer->getFieldsAsArray());
} catch (Exception $exception){
    echo $exception;
}

Create new customer

try{
    $customer = $client->customers()->create();
    $customer->name = "!APVTestCustomer";
    print_r($customer->save()->getFieldsAsArray());
} catch (Exception $exception){
    echo $exception;
}

Update customer

try {
    $customer = $client->customers()->create();
    $customer->id = 187873;
    $customer->name = "APVTestCustomerUpdate!";
    print_r($customer->update());
} catch (Exception $exception){
    echo $exception;
}

Add note to customer

try {
    $customer = $client->customers()->create()->getById(187873);
    $note = $customer->newNote();
    $note->note_type = NoteConstants::NOTE_TYPE_COMMON;
    $note->text = "New test note to CustomerModel";
    print_r($customer->addNote($note));
} catch (Exception $exception){
    echo $exception;
}

Task

Get all Task

try{
$task = $client->task();
print_r($task->getAll([]));
}catch (Exception $exception){
echo $exception;
}

Get task by id

try {
    $task = $client->task()->create()->getById(46510275);
    print_r($task);
} catch (Exception $exception){
    echo $exception;
}

Create new task

try{
    $task = $client->task()->create();
    $task->task_type_id = TaskConstants::TASK_TYPE_CONTACT;
    $task->text = "Test task for 28643682";
    $task->entity_id = 28643682;
    $task->entity_type = "leads";
    $task->complete_till = time() + 3600;
    print_r($task->save());
} catch (Exception $exception){
    echo $exception;
}

Update task

try {
    $task = $client->task()->create()->getById(50989199);
    $task->task_type_id = TaskConstants::TASK_TYPE_MEETING;
    $task->text = "Update task for 28643682";
    print_r($task->update());
} catch (Exception $exception){
    echo $exception;
}

Users

Get all users

try{
    $user = $client->users()->getAllUsers([]);
    print_r($user);
} catch (Exception $exception){
    echo $exception;
}

Get all roles

try{
    $user = $client->users()->getAllRoles([]);
    print_r($user);
} catch (Exception $exception){
    echo $exception;
}

Get user by id

try{
    $user = $client->users()->create()->getUserById(123);
    print_r($user);
} catch (Exception $exception){
    echo $exception;
}

Get role by id

try{
    $user = $client->users()->create()->getRoleById(123);
    print_r($user);
} catch (Exception $exception){
    echo $exception;
}

About

Library for amoCRM api

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages