Skip to content

Beachcasts/airtable-php

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Airtable for PHP

Basic SDK to deal with airtable records.

Installation

Tell composer to require this bundle by running:

composer require armetiz/airtable-php

Usage

$key   = "APP_KEY"; // Generated from : https://airtable.com/account
$base  = "BASE_ID"; // Find it on : https://airtable.com/api
$table = "TABLE_NAME"; // Find it on : https://airtable.com/api

$airtable = new Airtable($key, $base);

$records = $airtable->findRecords($table);

Available methods

  • Airtable::getRecord(string $table, string $id)
  • Airtable::createRecord(string $table, array $fields)
  • Airtable::setRecord(string $table, array $criteria = [], array $fields)
  • Airtable::updateRecord(string $table, array $criteria = [], array $fields)
  • Airtable::containsRecord(string $table, array $criteria = [])
  • Airtable::flushRecords(string $table)
  • Airtable::deleteRecord(string $table, array $criteria = [])
  • Airtable::findRecord(string $table, array $criteria = [])
  • Airtable::findRecords(string $table, array $criteria = [])

Example

Simple member indexer that encapsulate Airtable within simple API. Can be used to start a CRM on Airtable.

Note: Because Airtable doesn't allow schema manipulation using their public API, you should configure table using the WebUI with the following

  • Id : text
  • Firstname : text
  • Lastname : text
  • Email : email
  • CreatedAt : Date and time
  • Picture : Attachments
$key   = "APP_KEY"; // Generated from : https://airtable.com/account
$base  = "BASE_ID"; // Find it on : https://airtable.com/api
$table = "TABLE_NAME"; // Find it on : https://airtable.com/api

$airtable = new Airtable($key, $base);

$records = $airtable->findRecords($table);
use Armetiz\AirtableSDK\Airtable;

class MemberIndex
{
    /**
     * @var Airtable
     */
    private $airtable;

    private $table;

    /**
     * MemberIndexer constructor.
     *
     * @param Airtable $airtable
     * @param          $table
     */
    public function __construct(Airtable $airtable, $table)
    {
        $this->airtable = $airtable;
        $this->table    = $table;
    }

    public function clear()
    {
        $this->airtable->flushRecords($this->table);
    }

    public function save(array $data)
    {
        $this->guardData($data);

        $criteria = ["Id" => $data["id"]];
        $fields   = [
            "Id"                    => $data["id"],
            "Firstname"             => $data["firstName"],
            "Lastname"              => $data["lastName"],
            "Email"                 => $data["email"],
            "CreatedAt"             => (string)$data["createdAt"],
        ];

        if ($data["picture"]) {
            $record["Picture"] = [
                ["url" => $data["picture"]],
            ];
        }

        if ($this->airtable->containsRecord($this->table, $criteria)) {
            $this->airtable->updateRecord($this->table, $criteria, $fields);
        } else {
            $this->airtable->createRecord($this->table, $fields);
        }
    }

    public function delete($id)
    {
        $this->airtable->deleteRecord($this->table, ["Id" => $id]);
    }

    private function guardData(array $data)
    {
        $requiredKeys = [
            "id",
            "firstName",
            "lastName",
            "email",
            "picture",
            "createdAt",
        ];

        $availableKeys = array_keys($data);
        foreach ($requiredKeys as $requiredKey) {
            if (!in_array($requiredKey, $availableKeys)) {
                throw new \InvalidArgumentException(sprintf(
                    "Required keys '%s' from data is missing",
                    $requiredKey
                ));
            }
        }
    }
}

Testing

Not implemented yet.

License

This library is under the MIT license. See the complete license.

Credits

Author - Thomas Tourlourat

About

Manipulate AirTable API using PHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%