Convenient access to Airtable data using PHP.
PHP 5.6.0 and later.
You can install the library via Composer. Run the following command:
composer require knut/airtable-php
- 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);
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
TBD
Records can be queried by id
using the find
function on a table:
$company = $airtable->companies->find("rec3APJV3yRHlpHoA");
Records can be created using the create
function on a table:
$company = $airtable->companies->create([
'name' => 'New Company',
'website_url' => 'https://example.com',
]);
Records can be updated using the update
function on a table:
$company->name = 'An Updated Company';
$updatedRecord = $airtable->companies->update($company);
Records can be deleted using the delete
function on a table.
$result = $airtable->companies->delete($record);
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
.
- 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