Skip to content

Instantly share code, notes, and snippets.

@m-thomson
Last active March 13, 2021 11:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-thomson/3d9b6a96a65565681f7a1acd0bd22d49 to your computer and use it in GitHub Desktop.
Save m-thomson/3d9b6a96a65565681f7a1acd0bd22d49 to your computer and use it in GitHub Desktop.
Dump airtable contents to CSV
<?php
/*
* Totally simple way to dump an Airtable to CSV.
* Uses https://github.com/sleiman/airtable-php
*/
// When true, first field is the row number (starting with 1)
define("NUMBER_OUTPUT_ROWS", true);
include('lib/airtable-php/src/Airtable.php');
include('lib/airtable-php/src/Request.php');
include('lib/airtable-php/src/Response.php');
use TANIOS\Airtable\Airtable;
// Enter credentials here
$airtable = new Airtable(array(
'api_key' => '',
'base' => '',
));
$request = $airtable->getContent( 'Brands' );
$stdout = fopen('php://output', 'w');
$base = 0;
do {
$response = $request->getResponse();
foreach ($response['records'] as $n => $rec) {
if (!isset($headers)) {
$headers = array_keys((array)$rec->fields);
if (NUMBER_OUTPUT_ROWS) echo "#,";
echo implode(",",$headers)."\n";
}
$row = $n + 1 + $base;
$values = array();
foreach($headers as $header) {
$values[$header] = $rec->fields->$header;
}
if (NUMBER_OUTPUT_ROWS) array_unshift($values, $row);
fputcsv($stdout, $values);
}
$base += $n;
$request = $response->next();
} while( $request );
fclose($stdout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment