
Using cURL to get records from Airtable
Post #12, veröffentlicht am in- Web design
- How to
I was having a hard time, figuring out, how to use cURL to retrieve Airtable records with their new token-based API. Granted, I am a slow—albeit eager—learner. But their API documentation wasn’t that much help with references like curl -X POST https://api.airtable.com/v0/{YOUR_BASE_ID}/{YOUR_TABLE_ID_OR_TABLE_NAME}
. What’s that supposed to mean? Where does that go?
For future me, here is what I found.
Where I’m coming from
With Airtable’s previous API one was able to import table records into PHP with a simple
$json = file_get_contents('https://api.airtable.com/v0/{YOUR_BASE_ID}/{YOUR_TABLE_ID_OR_TABLE_NAME}?api_key={YOUR_API_KEY}');
Convert that to an array with $array = json_decode($json, true);
—boom. Done.
As for the cURL approach…
There is not that much helpful documentation available, that is actually digestable by a newbie to the matter. Most articles seem to explain using cURL in the command line, too.
Fishing in the mud
So, I started searching around the interwebs and stitching together what I could find.
This is, what a very basic version of a cURL call in PHP looks like:
$url = "https://someapi.com/…";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
Make that an Airtable API call:
$base_id = '{YOUR_BASE_ID}';
$table_id = '{YOUR_TABLE_ID_OR_TABLE_NAME}';
$token = '{YOUR_BEARER_TOKEN}';
$url = "https://api.airtable.com/v0/" . $base_id . "/" . $table_id;
$headers = [
'Content-Type: application/json',
'Authorization: Bearer ' . $token
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
Be aware, that you do need to generate the bearer token in your Airtable account. When logged in, go to airtable.com/create/tokens and hit “Create new token”. Name your new token, give it an appropriate scope—data.records:read
will do in our case—and select the base you want to access with it.
The above code does return something—a JSON object to be exact. But that something gets echoed in the browser. Not all that helpful. But it’s a start.
The full code example
Digging around some more, I figured, curlsetopt($ch, CURLOPTRETURNTRANSFER,1);
must be, what’s missing. And it was.
This is, what the full example from API call to array looks like:
$base_id = '{YOUR_BASE_ID}';
$table_id = '{YOUR_TABLE_ID_OR_TABLE_NAME}';
$token = '{YOUR_BEARER_TOKEN}';
$url = "https://api.airtable.com/v0/" . $base_id . "/" . $table_id;
$headers = [
'Content-Type: application/json',
'Authorization: Bearer ' . $token
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
$array = json_decode($result, true);
Much more complicated than file_get_contents
, but (supposedly) much more secure, too.
BTW, props to Airtable for announcing their switch to the new API a whole year up front. 🙏
And congratulation to cURL to its 25th anniversary, which is—wait for it—today. 🎉
Resources
Learn more about Airtable and register for a free account: https://www.airtable.com/
In January 2023 Airtable announced the “upcoming API keys deprecation period”.
This thread on StackOverflow turned out to be the most helpful in the end: https://stackoverflow.com/questions/30426047/correct-way-to-set-bearer-token-with-curl
cURL was released on March 20, 1998. Read more on Daniel Stenberg’s website: https://daniel.haxx.se/blog/2023/03/10/curl-25-years-online-celebration/