knut / airtable-php
The Airtable PHP library provides convenient access to the Airtable API using PHP.
Fund package maintenance!
knut
Requires
- php: >=7.2
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- phpunit/phpunit: ~8
- sempro/phpunit-pretty-print: ^1.2
- vlucas/phpdotenv: ^5.0
This package is auto-updated.
Last update: 2024-05-08 23:16:41 UTC
README
Convenient access to Airtable data using PHP.
Requirements
PHP 5.6.0 and later.
Installation
You can install the library via Composer. Run the following command:
composer require knut/airtable-php
Usage
Creating a Client
- Register for an Airtable account and find your API key here: https://airtable.com/account
- Create a new base and find your Base ID here: https://airtable.com/api
Your API key carries the same privileges as your user account, so be sure to keep it secret!
Now you can create your Airtable client:
$airtable = new \Airtable\Client($apiKey, $baseId);
Finding Records
Now you can query a set of records from a table in your base using the following syntax:
$records = $airtable->companies->find();
where companies
is the name of your table in your Airtable base. Note how you can access all table by named properties on the Airtable client.
Note that you can only request a maximimum of 100 records in a single query. To retrieve more records, use the "batch" feature below.
Finding Records by View-name
In Airtable you can create pre-defined filters in named Views. This can be a very useful starting point for many queries as you don't have to specify all the filters in code, just reference a named View.
This is how you find records for a named View:
$records = $airtable->companies->find(['view' => 'Name of your view in Airtable']);
Finding Records by Filtering Formula
You can use a Formula to filter your records by code using the filterByFormula
parameter.
In example to only include records where website_url
isn't empty you can use:
$records = $airtable->companies->find([ 'filterByFormula' => "NOT({website_url} = '')" ]);
Select only what you need
Very often you don't need all the data in a result set. You can use the fields
parameter to reduce the amount of data transferred.
For example, to only return data from name
and website_url
, specify these two field names in your parameters:
$records = $airtable->companies->find([ 'fields' => ['name', 'website_url'] ]);
Sorting
You can specify a sort order, limit and offset as part of our query.
Limit
TBD
Pagination
TBD
Batch Querying All Records
TBD
Finding a Record
Records can be queried by id
using the find
function on a table:
$company = $airtable->companies->find("rec3APJV3yRHlpHoA");
Creating a Record
Records can be created using the create
function on a table:
$company = $airtable->companies->create([ 'name' => 'New Company', 'website_url' => 'https://example.com', ]);
Updating a Record
Records can be updated using the update
function on a table:
$company->name = 'An Updated Company'; $updatedRecord = $airtable->companies->update($company);
Deleting a Record
Records can be deleted using the delete
function on a table.
$result = $airtable->companies->delete($record);
Development
Get Composer. For example, on Mac OS:
brew install composer
Install dependencies:
composer install
Run the test suite using phpunit
$ ./vendor/bin/phpunit
Code coverage report is found in build/coverage-report
.
Contributing
- Fork it (https://github.com/knut/airtable-php/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request