Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/remluben/cockpit-client
A PHP API client for a very simple straightforward readonly access to the cockpit headless CMS data.
https://github.com/remluben/cockpit-client
Last synced: 14 days ago
JSON representation
A PHP API client for a very simple straightforward readonly access to the cockpit headless CMS data.
- Host: GitHub
- URL: https://github.com/remluben/cockpit-client
- Owner: remluben
- License: mit
- Created: 2022-08-22T21:11:21.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-09-23T19:31:48.000Z (over 2 years ago)
- Last Synced: 2024-12-03T16:59:22.307Z (2 months ago)
- Language: PHP
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Cockpit Client
> **NOTE:**
>
> This library can be used with the new [Cockpit - Content Platform](https://github.com/Cockpit-HQ/Cockpit), a brand-new version of the Cockpit headless api-first CMS.
>
> As for this basic version the API client is readonly. This is likely going to change in future versions.A PHP API client for a very simple straightforward readonly access to the cockpit headless CMS data.
## Installation
You can install the package via composer:
```shell
composer require remluben/cockpit-client
```That's it. No more steps required in first place.
## Usage
This section provides usage information. We refer to an instance of the `\Remluben\CockpitClient\Client` class as *client*.
### Basics
At first ensure to setup your *client* with sensible Guzzle settings to avoid timeouts and bad performance. Next you can call any method the client provides you with to fetch data from your Cockpit installation.
```php
// Set up your basic HTTP client, which makes the HTTP requests to Cockpit under
// the hood.
$http = new GuzzleHttp\Client([
GuzzleHttp\RequestOptions::TIMEOUT => 2.0, // set an appropriate timeout that suits your application and server needs
GuzzleHttp\RequestOptions::DEBUG => false, // optionally enable in development mode
]);// Create an instance of the Cockpit client by passing all required data
$client = new Remluben\CockpitClient\Client(
$http, // The HTTP client
'https://url-to-cockpit.tld/api/', // The URL to the API of your Cockpit instance, i.e. https://url-to-cockpit.tld/api/
'API-1a45a0876a88fb3f042cc6524059a4a11bf3f163', // a static token / api-key for server-side usage, should not expire
);$results = [];
// Fetch your first content items, i.e. for content model *faqs*
try {
$results = $client->contentItems('faqs');
}
catch (\Remluben\CockpitClient\Exceptions\ClientException $e) {
// A client exception happens whenever
// - the HTTP client itself rises an exception
// - the Cockpit API returns non 2xx status codes or runs into issues
}
catch (\Remluben\CockpitClient\Exceptions\InvalidArgumentException $e) {
// For unintended method calls, invalid parameters or similar problems the
// client usually throws this exception
}// process your results, if any...
foreach ($results as $item) {
// do something here...
}
```### Error handling
Handling errors should be quite strait forward. Whenever something unexpected happens, a bad HTTP status code or an error is returned via API response the client reacts by throwing an Exception.
In other words, this means: only for HTTP status code *200* with a valid JSON response the request is considered as successful.
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## License
This software is released under the [MIT license](LICENSE.md).