pcbowers/php-airtable

PHP Airtable API 包装器

1.1.0 2019-08-12 21:32 UTC

This package is auto-updated.

Last update: 2024-09-13 08:43:09 UTC


README

PHP Airtable API 包装器

最近更新

新增 现在可以进行更新,如果需要,允许删除元素。有关详细信息,请参阅 updateRecords

新增 类型转换现在是默认选项。新的选择选项将自动创建,而不是抛出错误。

入门指南

按照以下步骤成功实施此 PHP Airtable 包装器。

注意:Airtable API 允许您在记录上实现基本的 CRUD 功能,但您无法更改表的架构。请使用他们的界面来更改架构。

获取您的 API 密钥和基础 ID

使用此包装器需要两个令牌:一个 API 令牌和一个基础 ID。按照以下步骤获取令牌

  1. 登录到 Airtable 并点击您的个人资料图片。
  2. 点击“帐户”。

  1. 滚动到 "<> API"。
  2. 复制个人 API 密钥。

  1. 导航到您想使用此包装器访问的基础。
  2. 点击帮助图标。
  3. 点击 "<> API 文档"。

  1. 查看 URL 并找到以 'app' 开头的 URL 部分。
  2. 复制您的基础 ID。

类安装

您可以使用 composer 或直接下载类。要使用 composer 下载,请运行以下命令

composer require pcbowers/php-airtable

如果您正在使用 composer,请运行自动加载器以将各种类包含到您的项目中

require 'vendor/autoload.php';

如果您直接下载了类,请包含 airtable.php 文件

include('../src/airtable.php');

初始化类

使用以下代码片段和您的 API 密钥和基础 ID 以开始使用包装器

use \pcbowers\Airtable\airtable;
$airtable = new Airtable(array(
    'api_key' => 'api_key',
    'base_id' => 'base_id'
));

示例

下面放置了一些示例,以帮助使用此包装器。这些示例假设类已经被初始化。

getApiKey & getBaseId

分别返回特定实例的 API 密钥和基础 ID。

代码示例
echo $airtable->getApiKey() . "<br />";
echo $airtable->getBaseId();
结果
your_api_key
your_base_id

listRecords

返回给定表的记录列表。不会返回记录中的空字段。

$table_name: String. Required.
$params: Array. All parameters are optional.

For more details on accepted parameters, see the airtable documentation.

已添加一个特殊参数,该参数不在 Airtable API 中,称为 checkOffset。此参数默认为 true,但可以设置为 false。它允许您返回一页的结果,而不是遍历所有结果。

这已经在包含 50,000 条记录和 31 列数据的 Airtable 基础上进行了测试。由于互联网连接和数据使用方式的差异,速度无法估计。但是,搜索范围越广,运行速度越慢。因此,在某些用例中,可能需要将 checkOffset 设置为 false 以提高加载时间,尽管您只能获取部分数据。或者,您可以设置 maxRecords 较低。理想情况下,您希望将请求保持在每页最多 1 页,这可以通过 maxRecords / pageSize 计算。建议此数字保持在 5 或更少以获得最佳加载速度。

代码模板
$airtable->listRecords($table_name, array(
    "fields" => array(strings),
    "filterByFormula" => string,
    "maxRecords" => number,
    "pageSize" => number,
    "sort" => array(objects),
    "view" => string,
    "cellFormat" => string,
    "timeZone" => string,
    "userLocale" => string,
    "checkOffset" => true
));
代码示例
print_r($airtable->listRecords("Users", array(
    "fields" => array("First Name", "Last Name"),
    "sort" => array(array("field" => "First Name", "direction" => "asc")),
    "maxRecords" => 100
)));
结果
Array of records. Contains:
    - up to 100 records
    - 'First Name' and 'Last Name' field
    - id of each record along with the created time
    - sorted by 'First Name ascending'

Because checkOffset was not set to false, all pages possible were returned.

retrieveRecord

从给定表中检索特定记录。不会返回记录中的空字段。

$table_name: String. Required.
$record_id: String. Required.
代码模板
$airtable->retrieveRecord($table_name, $record_id);
代码示例
print_r($airtable->retrieveRecord("Users", "recfauP0XQTTgXMQK"));
结果
Array with all the information from one record that is not empty.

createRecord

在给定表中创建记录。

$table_name: String. Required.
$data: Array. Optional (though recommended).
代码模板
$airtable->createRecord($table_name, array(
    "field_name1" => "field_value1",
    "field_name2" => "field_value2"...
));
代码示例
print_r($airtable->createRecord("Users", array(
    "First Name" => "Joe",
    "Last Name" => "Smith"
)));
结果
On success, the created record is returned with the field information and id of the record.

updateRecord

更新给定表中的特定记录。

$table_name: String. Required.
$record_id: String. Required.
$data: Array. Optional (though recommended).
$destructive: Boolean. Optional. Default false.
A value of false runs a PATCH update leaving data untouched if not edited.
A value of true runs a PUT update which destroys the instance and updates it with only the new data added.

**NEW** A value of "false" (not a boolean, but string "false") mixes the 2:
- A "false" value runs a PUT update which destroys the instance.
- However, unlike a normal PUT update, the new data is mixed with the old data.
- Fields left unmentioned will still remain in the record.
- Fields mentioned will be updated in the record.
- Fields mentioned and left as a blank string or array will be removed from the record.
- Fields that are not allowed to be edited must be included as empty values in $data (i.e. Formula Fields)
代码模板
$airtable->updateRecord($table_name, $record_id, array(
    "field_name1" => "field_value1",
    "field_name2" => "field_value2"...
), $destructive);
代码示例
print_r($airtable->updateRecord("Users", "recAkSf8l5IpITPV8", array(
    "First Name" => "Joe1",
    "Last Name" => "Smith1"
)));
结果
On success, updated record returned with the new information.
$destructive was not set to true, so the rest of the record's information remained intact.

deleteRecord

从给定表中删除特定记录。

$table_name: String. Required.
$record_id: String. Required.
代码模板
$airtable->deleteRecord($table_name, $record_id);
代码示例
print_r($airtable->deleteRecord("Users", "recU84ywe5m1md4wP"));
结果
On success, returns the fact that it was deleted along with the id of the deleted record.

getLastLog & getLog

错误记录功能得到支持,以便您的程序在请求错误时不会崩溃。任何需要Curl请求的功能,在失败时将返回false,在成功时将返回当前记录。此外,每个失败或成功的操作都会在包装实例中进行记录。以下代码演示了如何访问日志

代码
print_r($airtable->getLog());
echo "<br />";
print_r($airtable->getLastLog());
结果
your_entire_log
last_entry_of_log