PHP code example of asciisd / zoho-v8

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

    

asciisd / zoho-v8 example snippets


use Asciisd\Zoho\Models\ZohoContact;

$contact = ZohoContact::create([
    'First_Name' => 'John',
    'Last_Name' => 'Doe',
    'Email' => '[email protected]',
    'Phone' => '+1234567890',
]);

$contact = ZohoContact::find('4150868000000624001');

ZohoContact::update('4150868000000624001', [
    'Phone' => '+0987654321',
    'Title' => 'Senior Developer',
]);

ZohoContact::delete('4150868000000624001');

$contacts = ZohoContact::all([
    'per_page' => 200,
    'page' => 1,
]);

foreach ($contacts as $contact) {
    echo $contact['Full_Name'];
}

// Search by email
$contacts = ZohoContact::searchByEmail('[email protected]');

// Search by phone
$contacts = ZohoContact::searchByPhone('+1234567890');

// Custom search
$contacts = ZohoContact::search('(Last_Name:starts_with:Doe)');

$contact = ZohoContact::upsert([
    'First_Name' => 'Jane',
    'Last_Name' => 'Smith',
    'Email' => '[email protected]',
], ['Email']); // Duplicate check by Email

use Asciisd\Zoho\Facades\Zoho;

// Create a contact
$contact = Zoho::contacts()->create([...]);

// Create a lead
$lead = Zoho::leads()->create([...]);

// Create a deal
$deal = Zoho::deals()->create([...]);

$deals = ZohoContact::getRelatedRecords('4150868000000624001', 'Deals');

ZohoContact::updateMultiple([
    ['id' => '123', 'Phone' => '111'],
    ['id' => '456', 'Phone' => '222'],
]);

ZohoContact::deleteMultiple(['123', '456', '789']);

$deletedContacts = ZohoContact::getDeletedRecords([
    'type' => 'permanent',
]);

ZohoLead::convert('4150868000000624001', [
    'overwrite' => true,
    'notify_lead_owner' => true,
    'notify_new_entity_owner' => true,
]);

$count = ZohoContact::count();

$clonedContact = ZohoContact::clone('4150868000000624001');

$fields = ZohoContact::getFieldMetadata();

// Each field contains information like:
// - api_name
// - field_label
// - data_type
// - read_only
// - 

// Fetch only specific fields
$contact = ZohoContact::find('123', [
    'fields' => 'id,First_Name,Last_Name,Email'
]);

// Search with specific fields
$contacts = ZohoContact::all([
    'fields' => 'Full_Name,Email,Phone',
    'per_page' => 50,
]);

// Clear cache for specific module
ZohoContact::clearFieldCache();

// Clear cache for all modules
ZohoContact::clearAllFieldCache();

use Asciisd\Zoho\Events\ZohoRecordCreated;
use Asciisd\Zoho\Events\ZohoRecordUpdated;
use Asciisd\Zoho\Events\ZohoRecordDeleted;
use Asciisd\Zoho\Events\ZohoWebhookReceived;

// Listen to all webhooks
Event::listen(ZohoWebhookReceived::class, function ($event) {
    $module = $event->module;
    $eventType = $event->event;
    $data = $event->getData();
    
    // Handle webhook
});

// Listen to specific events
Event::listen(ZohoRecordCreated::class, function ($event) {
    $module = $event->module; // e.g., 'Contacts'
    $record = $event->getData();
    $recordId = $event->getRecordId();
    
    // Handle record creation
});

Event::listen(ZohoRecordUpdated::class, function ($event) {
    // Handle record update
});

Event::listen(ZohoRecordDeleted::class, function ($event) {
    // Handle record deletion
});

use Asciisd\Zoho\Traits\SyncsWithZoho;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use SyncsWithZoho;
    
    protected $fillable = ['name', 'email', 'phone'];
    
    /**
     * Specify which Zoho module to sync with.
     */
    protected function getZohoModule(): string
    {
        return 'Contacts';
    }
}

// Automatically creates a Contact in Zoho CRM (queued)
$user = User::create([
    'name' => 'John Doe',
    'email' => '[email protected]',
    'phone' => '+1234567890',
]);

// Automatically updates the Contact in Zoho CRM (queued)
$user->update(['phone' => '+0987654321']);

// Automatically deletes the Contact in Zoho CRM (queued)
$user->delete();

class User extends Authenticatable
{
    use SyncsWithZoho;
    
    protected $fillable = ['name', 'email', 'phone', 'company'];
    
    protected function getZohoModule(): string
    {
        return 'Contacts';
    }
    
    /**
     * Map Laravel model fields to Zoho CRM fields.
     */
    protected function getZohoFieldMapping(): array
    {
        return [
            'name' => 'Full_Name',
            'email' => 'Email',
            'phone' => 'Phone',
            'company' => 'Account_Name',
        ];
    }
}

use Asciisd\Zoho\Traits\SyncsWithZoho;
use Illuminate\Database\Eloquent\Model;

class DemoAccount extends Model
{
    use SyncsWithZoho;
    
    protected $fillable = [
        'name',
        'email',
        'phone',
        'country',
        'company',
    ];
    
    protected function getZohoModule(): string
    {
        return 'Leads';
    }
    
    protected function getZohoFieldMapping(): array
    {
        return [
            'name' => 'Last_Name',
            'email' => 'Email',
            'phone' => 'Phone',
            'company' => 'Company',
            // 'country' will map automatically with the same name
        ];
    }
}

protected function shouldSyncToZoho(): bool
{
    // Only sync verified users
    if ($this->email_verified_at === null) {
        return false;
    }
    
    // Only sync users with specific role
    if (!$this->hasRole('customer')) {
        return false;
    }
    
    return parent::shouldSyncToZoho();
}

// Import data without syncing to Zoho
User::withoutZohoSync(function () {
    User::create(['name' => 'John', 'email' => '[email protected]']);
    User::create(['name' => 'Jane', 'email' => '[email protected]']);
    // ... import 1000 more users
});

// Or disable for a single operation
User::withoutZohoSync(fn () => $user->update(['internal_notes' => 'test']));

// Force immediate sync to Zoho
$user->syncToZohoNow('create');
$user->syncToZohoNow('update');

$user = User::find(1);

// Get the Zoho record ID
$zohoRecordId = $user->getZohoRecordId();

// Access the full sync relationship
$zohoSync = $user->zohoSync;
echo $zohoSync->zoho_record_id;
echo $zohoSync->zoho_module;
echo $zohoSync->last_synced_at;

'sync' => [
    // Enable or disable automatic syncing globally
    'enabled' => env('ZOHO_SYNC_ENABLED', true),
    
    // Queue connection to use for sync jobs
    'queue' => env('ZOHO_SYNC_QUEUE', 'default'),
    
    // Number of retry attempts
    'retry_attempts' => env('ZOHO_SYNC_RETRY_ATTEMPTS', 3),
    
    // Backoff delays between retries (in seconds)
    'retry_backoff' => [60, 120, 300], // 1 min, 2 min, 5 min
],

// Check logs for sync failures
tail -f storage/logs/laravel.log | grep "Zoho sync"

return [
    'client_id' => env('ZOHO_CLIENT_ID'),
    'client_secret' => env('ZOHO_CLIENT_SECRET'),
    'redirect_uri' => env('ZOHO_REDIRECT_URI'),
    'refresh_token' => env('ZOHO_REFRESH_TOKEN'),
    'access_token' => env('ZOHO_ACCESS_TOKEN'),
    
    // Environment: production, sandbox, developer
    'environment' => env('ZOHO_ENVIRONMENT', 'production'),
    
    // Data Center: US, EU, IN, CN, JP, AU, CA
    'data_center' => env('ZOHO_DATA_CENTER', 'US'),
    
    // Token Storage: cache, database, both
    'token_storage' => env('ZOHO_TOKEN_STORAGE', 'both'),
    
    'cache_driver' => env('ZOHO_CACHE_DRIVER', 'file'),
    'cache_ttl' => env('ZOHO_CACHE_TTL', 3600),
    
    'webhook_secret' => env('ZOHO_WEBHOOK_SECRET'),
    
    'pagination' => [
        'per_page' => env('ZOHO_PER_PAGE', 200),
        'max_records' => env('ZOHO_MAX_RECORDS', 200),
    ],
];

use Asciisd\Zoho\Exceptions\ZohoException;
use Asciisd\Zoho\Exceptions\ZohoAuthException;
use Asciisd\Zoho\Exceptions\ZohoApiException;
use Asciisd\Zoho\Exceptions\ZohoTokenException;

try {
    $contact = ZohoContact::find('invalid_id');
} catch (ZohoApiException $e) {
    // Handle API error
    echo $e->getMessage();
} catch (ZohoAuthException $e) {
    // Handle authentication error
} catch (ZohoTokenException $e) {
    // Handle token error
}
bash
php artisan vendor:publish --tag=zoho-config
php artisan vendor:publish --tag=zoho-migrations
bash
php artisan migrate
bash
php artisan zoho:setup
bash
php artisan zoho:setup
bash
# Show authentication status
php artisan zoho:auth status

# Show authorization URL
php artisan zoho:auth url

# Refresh access token
php artisan zoho:auth refresh

# Revoke access token
php artisan zoho:auth revoke
bash
php artisan vendor:publish --tag=zoho-migrations
php artisan migrate
bash
# Test Contacts module
php artisan zoho:test Contacts

# Test specific operations
php artisan zoho:test Leads --operation=create
php artisan zoho:test Accounts --operation=read
bash
php artisan zoho:token:refresh
bash
php artisan zoho:auth status