|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Webflow; |
| 4 | + |
| 5 | +use Webflow\WebflowException; |
| 6 | +use GuzzleHttp\Client; |
| 7 | + |
| 8 | +class Api { |
| 9 | + |
| 10 | + const WEBFLOW_API_ENDPOINT = 'https://api.webflow.com'; |
| 11 | + |
| 12 | + private $client; |
| 13 | + |
| 14 | + function __construct( |
| 15 | + $token, |
| 16 | + $version = '1.0.0' |
| 17 | + ) { |
| 18 | + if (empty($token)) { |
| 19 | + throw new WebflowException('token'); |
| 20 | + } |
| 21 | + |
| 22 | + $this->client = new Client([ |
| 23 | + 'base_uri' => self::WEBFLOW_API_ENDPOINT, |
| 24 | + 'headers' => [ |
| 25 | + 'Authorization' => "Bearer {$token}", |
| 26 | + 'accept-version' => $version, |
| 27 | + 'Accept' => 'application/json', |
| 28 | + 'Content-Type' => 'application/json', |
| 29 | + ] |
| 30 | + ]); |
| 31 | + |
| 32 | + return $this; |
| 33 | + } |
| 34 | + |
| 35 | + // Meta |
| 36 | + |
| 37 | + public function info() { |
| 38 | + return $this->client->get('/info')->getBody(); |
| 39 | + } |
| 40 | + |
| 41 | + public function sites() { |
| 42 | + return $this->client->get('/sites')->getBody(); |
| 43 | + } |
| 44 | + |
| 45 | + public function site(string $siteId) { |
| 46 | + return $this->client->get("/sites/{$siteId}")->getBody(); |
| 47 | + } |
| 48 | + |
| 49 | + public function domains(string $siteId) { |
| 50 | + return $this->client->get("/sites/{$siteId}/domains")->getBody(); |
| 51 | + } |
| 52 | + |
| 53 | + public function publishSite(string $siteId, array $domains) { |
| 54 | + return $this->client->post(`/sites/${siteId}/publish`, $domains); |
| 55 | + } |
| 56 | + |
| 57 | + // Collections |
| 58 | + |
| 59 | + public function collections(string $siteId) { |
| 60 | + return $this->client->get("/sites/{$siteId}/collections")->getBody(); |
| 61 | + } |
| 62 | + |
| 63 | + public function collection(string $collectionId) { |
| 64 | + return $this->client->get("/collections/{$collectionId}")->getBody(); |
| 65 | + } |
| 66 | + |
| 67 | + // Items |
| 68 | + |
| 69 | + public function items(string $collectionId) { |
| 70 | + return $this->client->get("/collections/{$collectionId}/items")->getBody(); |
| 71 | + } |
| 72 | + |
| 73 | + public function item(string $collectionId, string $itemId) { |
| 74 | + return $this->client->get("/collections/{$collectionId}/items/{$itemId}")->getBody(); |
| 75 | + } |
| 76 | + |
| 77 | + public function createItem(string $collectionId, array $data) { |
| 78 | + return $this->client->post("/collections/{$collectionId}/items", [ |
| 79 | + 'json' => [ |
| 80 | + 'fields' => $data, |
| 81 | + ], |
| 82 | + ])->getBody(); |
| 83 | + } |
| 84 | + |
| 85 | + public function updateItem(string $collectionId, string $itemId, array $data) { |
| 86 | + return $this->client->put("/collections/{$collectionId}/items/{$itemId}", $data); |
| 87 | + } |
| 88 | + |
| 89 | + public function removeItem(string $collectionId, $itemId) { |
| 90 | + return $this->client->delete("/collections/{$collectionId}/items/{$itemId}")->getBody(); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments