Last active
March 13, 2021 11:33
-
-
Save m-thomson/3d9b6a96a65565681f7a1acd0bd22d49 to your computer and use it in GitHub Desktop.
Dump airtable contents to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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