sliemian / airtable-php
Airtable API 的 PHP 封装器
Requires
- php: >=8.0
- davedevelopment/stiphle: ^0.9.2
README
Airtable API 的 PHP 客户端。欢迎评论、请求或错误报告。
开始使用
请注意,Airtable 不允许通过其公共 API 操作模式,您必须使用其界面创建表格。
在 Airtable 界面中创建您的库后,打开 API 文档以获取库 ID。
库 ID 是以 'app' 开头的代码,后面跟字母或数字的混合(例如 appsvqGDFCwLC3I10)。
安装
如果您使用 Composer,可以运行以下命令
composer require sleiman/airtable-php
您也可以直接下载并解压到您的网页目录中。
将封装器添加到项目中
如果您使用 Composer,运行自动加载器
require 'vendor/autoload.php';
或者包含 Airtable.php 文件
include('../src/Airtable.php'); include('../src/Request.php'); include('../src/Response.php');
初始化类
use \TANIOS\Airtable\Airtable; $airtable = new Airtable(array( 'api_key' => 'API_KEY', 'base' => 'BASE_ID' ));
获取表中的所有条目
我们正在获取表格 "Contacts" 中的所有条目。
$request = $airtable->getContent( 'Contacts' ); do { $response = $request->getResponse(); var_dump( $response[ 'records' ] ); } while( $request = $response->next() ); print_r($request);
使用参数进行筛选、排序等
// You don't have to use all the params, they are added as a reference $params = array( "filterByFormula" => "AND( Status = 'New' )", "sort" => array(array('field' => 'Count', 'direction' => "desc")), "maxRecords" => 175, "pageSize" => 50, "view" => "Name of your View" ); $request = $airtable->getContent( 'Contacts', $params); do { $response = $request->getResponse(); var_dump( $response[ 'records' ] ); } while( $request = $response->next() ); print_r($request);
创建新条目
我们将在 "Contacts" 表中创建新条目
// Create an array with all the fields you want $new_contact_details = array( 'Name' =>"Contact Name", 'Address' => "1234 Street Name, City, State, Zip, Country", 'Telephone #' => '123-532-1239', 'Email' =>'email@domain.com', ); // Save to Airtable $new_contact = $airtable->saveContent( "Contacts", $new_contact_details ); // The ID of the new entry echo $new_contact->id; print_r($new_contact);
现在支持批量创建,文档见下文
更新联系人
使用条目 ID 更新条目
$update_contact_details = array( 'Telephone #' => '514-123-2942', ); $update_contact = $airtable->updateContent("Contacts/{entry-id}",$update_contact_details); print_r($update_contact);
现在支持批量更新,文档见下文
扩展关系(预加载)
响应将包括来自其他表链接的记录的所有信息。在此示例中,通过单个调用,"Customer Details" 字段将填充 "Customer Details" 表的关系。
当您未传递关联数组时,我们假设字段和表名相同。
$expended = $airtable->getContent( "Customers/recpJGOaJYB4G36PU", false, [ 'Customer Details' ] );
如果出于某种原因,字段名称与表名不同,您可以传递关联数组。
$expended = $airtable->getContent( "Customers/recpJGOaJYB4G36PU", false, [ 'Field Name' => 'Table Name', 'Customer Meetings' => 'Meetings' ] );
我们听说您喜欢扩展关系,因此现在您可以扩展扩展关系。以下是可能的操作。
$expend_expended = $airtable->getContent( "Customers/recpJGOaJYB4G36PU", false, [ 'Customer Details', 'Meetings' => [ 'table' => 'Meetings', 'relations' => [ 'Calendar' => 'Calendar', 'Door' => [ 'table' => 'Doors', 'relations' => [ 'Added By' => 'Employees' ] ] ] ] ] );
但请注意,加载过多的关系可能会显著增加响应时间。
删除条目
使用条目 ID 删除条目
$delete_contact = $airtable->deleteContent("Contacts/{entry-id}");
现在支持批量删除,文档见下文
快速检查(新功能)
通过一行代码查找记录或多个记录。当您想知道用户是否已注册或是否使用了相同的 SKU 时很有用。响应将返回 "count" 和 "records"。
$check = $airtable->quickCheck("Contacts",$field,$value); $check = $airtable->quickCheck("Contacts","Email","jon@wordlco.com"); if($check->count > 0){ // the value is already there var_dump($check->records); } else { // it's not there }
批量创建、更新、删除
Airtable API 现在允许在单个 API 请求中创建、更新和删除 10 条记录。
创建
$content = $a->saveContent( 'Links', [ [ 'fields' => [ 'Name' => 'Tasty' ] ], [ 'fields' => [ 'Name' => 'Yolo' ] ] ] );
更新
$update = []; foreach ( $content->records as $record ) { $update[] = [ 'id' => $record->id, 'fields' => [ 'Slug' => strtolower( $record->fields->Name ) ] ]; } $response = $a->updateContent( 'Links', $update );
删除
$delete = []; foreach ( $response->records as $record ) { $delete[] = $record->id; } $response = $a->deleteContent( 'Links', $delete );
致谢
版权 (c) 2019 - 由 Sleiman Tanios & Guillaume Laliberté 编程