koalati/webflow-api-client

Webflow REST API 的客户端。管理网站、集合、电商、成员等。

v0.5.0 2023-03-04 21:55 UTC

This package is auto-updated.

Last update: 2024-09-11 01:00:58 UTC


README

Latest Version Software License Build Status Latest real-life test Total Downloads

本软件包提供了对 Webflow REST API 的客户端。

工作进行中

本软件包仍在开发中。

在API客户端被视为完整和准备好之前,必须完成以下问题

如果您想帮忙,请查看下面的 贡献 部分,选择一个问题并提交一个拉取请求!

要求

本软件包需要PHP 8.1 或更高版本。

安装

要安装,请使用composer

composer require koalati/webflow-api-client

入门

1. 使用Webflow进行身份验证

请参阅 Webflow身份验证文档

要访问API,您需要提供一个 access_token 以进行Webflow身份验证。您可以通过以下两种方式之一获取该令牌。

  • 利用OAuth允许您的应用程序的用户授权您的应用程序访问他们的Webflow帐户和数据。 [...]
  • 发行站点API令牌,允许您的应用程序完全访问您的个人帐户。 查看我们关于站点API令牌的指南

我们建议您选择OAuth身份验证。 要设置OAuth流程,我们建议您使用我们的 koalati/oauth2-webflow 软件包。

如果您想使用Webflow的站点API令牌,请遵循上面链接的文档页面上的步骤以获取您的访问令牌。请注意,如果您选择使用站点API令牌,您可能无法访问API的更多功能。

无论如何,请确保所有访问令牌都安全(例如,以加密格式存储它们)。

2. 使用您的访问令牌实例化API客户端

<?php

use Koalati\Webflow\Api\Client;

// Fetch your access token 
// @TODO: change this to get the token from where you stored it.
$accessToken = getenv("WEBFLOW_API_ACCESS_TOKEN");

// Instanciate the API client
$client = new Client($accessToken);

3. 开始与API交互!

// Fetch the list of sites
$sites = $client->listSites();

// Fetch a specific site
$siteId = "6114382d5af6775b0abebe2c";
$specificSite = $client->getSite($siteId);

// Publish a site
$client->publishSite($siteId);

// List all collections for a site
$collections = $client->listCollections($siteId);

// Fetch and iterate over collection items
$items = $client->listCollectionItems($collections[0]);
foreach ($items as $item) {
	echo $item->name . "\n";
}

如果您与单个网站交互...

如果您与单个Webflow网站交互,您可以使用 SiteClient 而不是基本的 Client 来使您的代码更干净。

这两个客户端提供完全相同的功能(Client 实际上是 SiteClient 内部使用的),但它可以节省您在每次调用时传递网站ID的时间。

以下是一个示例

<?php

use Koalati\Webflow\Api\SiteClient;

// Fetch your access token 
// @TODO: change this to get the token from where you stored it.
$accessToken = getenv("WEBFLOW_API_ACCESS_TOKEN");

// Instanciate the API client
$client = new SiteClient($accessToken, "your-site-id");

$domains = $client->listDomains();
$collections = $client->listCollections();
// etc...

用法

分页

分页端点将返回一个 PaginatedList 实例。这将防止您进行比实际需要的更多API调用。

默认情况下,总是获取第一页的结果。当您遍历列表时,只有在您真正到达那个点时,才会进行额外的API调用以加载数据。

$items = $client->listCollectionItems('somecollectionid');

foreach ($items as $item) {
	// The first 100 items are already loaded.
	// Once you reach the 101st item, an API call will be made automatically to load the next batch.
	// Same thing one you reach the 201st item, and so on and so forth.
}

如果您希望一开始就获取所有数据,可以使用 PaginatedList::fetchAll() 方法,该方法加载数据并将其作为数组返回。

示例。

$itemList = $client->listCollectionItems('somecollectionid');
$itemsArray = $itemList->fetchAll();

var_dump($itemsArray);
// array(561) { 
//	[0]=> object(Koalati\Webflow\Model\CollectionItem)#1 (13) { ... } }
//	[1]=> object(Koalati\Webflow\Model\CollectionItem)#2 (13) { ... } }
//  ...
//	[559]=> object(Koalati\Webflow\Model\CollectionItem)#560 (13) { ... } }
//	[560]=> object(Koalati\Webflow\Model\CollectionItem)#561 (13) { ... } }
// }

更新数据

可以通过API进行更新的模型,例如集合项和用户,跟踪自己的更改:您只需通过它们的模型实例进行更新,并在准备好时使用模型调用它们的更新端点即可。

示例。

$item = $client->getCollectionItem('somecollectionid', 'someitemid');

// Update the data on the model
$item->setFieldValue('name', 'My Updated Item Name');
$item->setFieldValue('some-custom-field', 'Another update value');

// Make the API call - changes you made will be automatically detected and sent
$updatedItem = $client->updateCollectionItem('somecollectionid', $item);

// $updatedItem now holds the updated version of the item, as returned by Webflow's API.

贡献

请参阅CONTRIBUTING获取详细信息。

致谢

本包的核心是由Koalati开发的,一个面向网页开发者和机构的QA平台。

查看其他帮助维护和改进此包的贡献者:所有贡献者

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件