Write to an Airtable with PHP

Lately I’ve been rediscovering the joy of PHP. Also been helping an MVP get off the ground which uses a lot of nocode/lowcode bits and pieces and miraculously puts them together. Anyway, I had to write to an Airtable table with PHP and some quick googling didn’t find an example to copy-paste so I’m writing […]


This content originally appeared on phpied.com and was authored by Stoyan


Lately I've been rediscovering the joy of PHP. Also been helping an MVP get off the ground which uses a lot of nocode/lowcode bits and pieces and miraculously puts them together.

Anyway, I had to write to an Airtable table with PHP and some quick googling didn't find an example to copy-paste so I'm writing this one for posterity.

So I have a table called people which has Name and Email fields. I want to write to it programmatically. Airtable has this pretty cool documentation which uses existing data from your real tables as examples. Search "airtable api" and you'll find your way.

There you'll see things like this:

Here people is the table name and blarghblahbla is the the AirTable app ID (obfuscated). This is generated for you so no need to worry about it, just copy. The question is to turn it into PHP code.

Oh, you'll also need your Airtable key which you get from your account:

Armed with these prerequisites, here's the PHP code that uses cURL to write to the table:

// get this from your account
$airtable_key = 'keyeKanyeOladiOblada';

// URL generated by the docs
$url = 'https://api.airtable.com/v0/blarghblahbla/people';
$headers = [
  'Content-Type: application/json',
  'Authorization: Bearer ' . $airtable_key,    
];

// this is the JSON-encoded record to write to the table
$post = '{"fields": {"Name": "Stoyan", "Email": "ssttoo@ymail.com"}}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);

Et voilà!


This content originally appeared on phpied.com and was authored by Stoyan


Print Share Comment Cite Upload Translate Updates
APA

Stoyan | Sciencx (2022-02-22T00:59:34+00:00) Write to an Airtable with PHP. Retrieved from https://www.scien.cx/2022/02/22/write-to-an-airtable-with-php/

MLA
" » Write to an Airtable with PHP." Stoyan | Sciencx - Tuesday February 22, 2022, https://www.scien.cx/2022/02/22/write-to-an-airtable-with-php/
HARVARD
Stoyan | Sciencx Tuesday February 22, 2022 » Write to an Airtable with PHP., viewed ,<https://www.scien.cx/2022/02/22/write-to-an-airtable-with-php/>
VANCOUVER
Stoyan | Sciencx - » Write to an Airtable with PHP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/22/write-to-an-airtable-with-php/
CHICAGO
" » Write to an Airtable with PHP." Stoyan | Sciencx - Accessed . https://www.scien.cx/2022/02/22/write-to-an-airtable-with-php/
IEEE
" » Write to an Airtable with PHP." Stoyan | Sciencx [Online]. Available: https://www.scien.cx/2022/02/22/write-to-an-airtable-with-php/. [Accessed: ]
rf:citation
» Write to an Airtable with PHP | Stoyan | Sciencx | https://www.scien.cx/2022/02/22/write-to-an-airtable-with-php/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.