Skip to content

PHP API

Shahjahan Jewel edited this page Nov 9, 2022 · 12 revisions

FluentCRM has the following PHP api functions so you can easily add/modify contacts.

<?php

    $contactApi = FluentCrmApi('contacts');
    /*
    * Find a contact by Email or Contact ID
    * You can find a contact by email or contact id (ContactId is not the user ID).
    * @return: false or Contact Model Object
    */
    $contact = $contactApi->getContact($emailOrContactId);

    /*
    * Find a contact by user_id
    * You can find a contact by user_id
    */
    $contact = $contactApi-> getContactByUserRef($userId);

    /*
    * get current logged in user's contact profile
    *
    */
    $contact = $contactApi->getCurrentContact();

Accessing Contact Properies

<php
    /*
    * Accessing contact properties
    */
    $contact->first_name;
    $contact->last_name;
    $contact->email;
    $contact->tags; // Tags array that are assigned to the contact 
    $contact->lists; // Lists array that are assigned to the contact

Updating Contact Properies

<?php

    /*
    * Updating contact properties
    */
    $contact->first_name = 'New First Name';
    $contact->last_name = 'Last Name';
    $contact->save();

    /*
    * Adding tags to a contact
    */
    $tagIds = [1,2,3];
    if($contact) {
       $contact->attachTags($tagIds);
    }
  

    /*
    * Adding Lists to a contact
    */
    $listIds = [2,3];
    if($contact) {
       $contact->attachLists($listIds);
    }

    /*
    * Removing tags from a contact
    */
    $tagIds = [1,2,3];
    $contact->detachTags($tagIds);

    /*
    * Adding Lists to a contact
    */
    $listIds = [2,3];
    $contact->detachLists($listIds);

Creating or Updating a new contact

<?php

   $contactApi = FluentCrmApi('contacts');

    /*
    * Update/Insert a contact
    * You can create or update a contact in a single call
    */

    $data = [
        'first_name' => 'Jhon',
        'last_name' => 'Doe',
        'email' => '[email protected]', // requied
        'status' => 'pending',
        'tags' => [1,2,3], // tag ids as an array
        'lists' => [4] // list ids as an array
    ];

    $contact = $contactApi->createOrUpdate($data);

    // send a double opt-in email if the status is pending
    if($contact->status == 'pending') {
        $contact->sendDoubleOptinEmail();
    }

Accessing lists

<?php

   /*
    * List is a contact segment.
    */
    $listApi = FluentCrmApi('lists');

    // Get all the lists
    $allLists = $listApi->all(); // array of all the lists and each list is an object

    // get a single list
    $list = $listApi->find(1); // finding the list id 1

    // accessing a list
    $listId = $list->id;
    $listTitle = $list->title;
    $listSlug = $list->slug;

Accessing Tags

<?php
    /*
    * Tag is a contact segment. You can easily access those
    */

    $tagApi = FluentCrmApi('tags');

    // Get all the tags
    $allTags = $tagApi->all(); // array of all the lists and each list is an object

    // get a single tag
    $tag = $tagApi->find(1); // finding the list id 1

    // accessing a tag
    $tagId = $tag->id;
    $tagTitle = $tag->title;
    $tagSlug = $tag->slug;

    // Create tags as bulk
    // If tag already exist with with same title then it will be updated
    // This will return the array of Tag model
    $tags = FluentCrmApi('tags')->importBulk([
            [
                'title' => 'Your Tag 1'
            ],
            [
                'title' => 'Your Tag 2'
            ]
        ]);

Filtering Contacts

    $contactApi = FluentCrmApi('contacts');

    // get Subscribed Contacts
    $subscribedContacts = $contactApi->getInstance()->where('status', 'subscribed')->get(); // you can also use paginate() instead of get();

    // Get both pending and upsubscribed contacts
    $contacts = $contactApi->getInstance()->whereIn('status', ['unsubscribed', 'pending'])->get();

    // Get contacts by tag ids
    $tagIds = [1,2];
    $tagOneTwoContacts = $contactApi->getInstance()->filterByTags($tagIds)->get();

    // Get contacts by list ids
    $listIds = [1,2];
    $ListOneTwoContacts = $contactApi->getInstance()->filterByLists($listIds)->get();

    // search contacts
    $searchResult = $contactApi->getInstance()->searchBy('search_string')->get();
    

Check if a contact is in any provided tags

    $contactApi = FluentCrmApi('contacts');
    // get current logged in contact
    $contact = $contactApi->getCurrentContact();
     
    $targetTagIds = [1,2,3];
    $isInTags = ($contact && $contact->hasAnyTagId($targetTagIds));