shevabam / jsonbin-sdk-php
PHP SDK for JsonBin.io API
dev-main
2025-02-27 21:48 UTC
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.0
- psr/http-client: ^1.0
- psr/log: ^3.0
This package is auto-updated.
Last update: 2025-02-27 21:48:58 UTC
README
A modern PHP SDK for the JsonBin.io API. This SDK uses PHP 8.2+ and follows development best practices to provide you with a clean and fluent interface to the JsonBin.io API.
Table of Contents
- Requirements
- Installation
- Usage
- Error Handling
- Configuration Options
Requirements
- PHP 8.2 or higher
- Composer
Installation
composer require shevabam/jsonbin-sdk
Usage
Initialization
<?php require 'vendor/autoload.php'; use JsonBinSDK\JsonBinClient; // Initialize the client with your Master Key $client = new JsonBinClient('YOUR_API_KEY');
Working with Bins
Create a bin
// Create a simple bin $data = ['name' => 'John Doe', 'age' => 30]; $result = $client->bins()->create($data); // Create a bin with options $result = $client->bins()->create($data, [ 'private' => true, 'name' => 'My test bin', 'collectionId' => 'COLLECTION_ID' ]);
Read a bin
$binId = 'BIN_ID'; $bin = $client->bins()->read($binId);
Update a bin
$binId = 'BIN_ID'; $updatedData = ['name' => 'Jane Doe', 'age' => 31]; $result = $client->bins()->update($binId, $updatedData);
Delete a bin
$binId = 'BIN_ID'; $result = $client->bins()->delete($binId);
Working with Collections
Create a collection
$result = $client->collections()->create('My Collection');
Read a collection
$collectionId = 'COLLECTION_ID'; $collection = $client->collections()->read($collectionId);
Update a collection
$collectionId = 'COLLECTION_ID'; $result = $client->collections()->update($collectionId, 'New Collection Name');
Delete a collection
$collectionId = 'COLLECTION_ID'; $result = $client->collections()->delete($collectionId);
List bins in a collection
$collectionId = 'COLLECTION_ID'; $bins = $client->collections()->listBins($collectionId);
List bins without collection (uncategorized)
$bins = $client->collections()->listUncategorizedBins();
Add a bin to a collection
$collectionId = 'COLLECTION_ID'; $binId = 'BIN_ID'; $result = $client->collections()->addBin($collectionId, $binId);
Remove a bin from a collection
$collectionId = 'COLLECTION_ID'; $binId = 'BIN_ID'; $result = $client->collections()->removeBin($collectionId, $binId);
Working with Access Keys
Create an access key
$result = $client->accessKey()->create('My New Access Key 2', [ 'read' => true, 'update' => true, 'delete' => true, 'create' => true, ]);
List access keys
$bins = $client->accessKey()->list();
Delete an access key
$accessKey = 'abc123'; $bins = $client->accessKey()->delete($accessKey);
Working with Logs
List logs
$bins = $client->log()->list();
Error Handling
The SDK throws JsonBinSDK\Exception\ApiException
when an API error occurs:
use JsonBinSDK\Exception\ApiException; try { $bin = $client->bins()->read('non_existent_id'); } catch (ApiException $e) { echo 'API Error: ' . $e->getMessage(); }
Configuration Options
You can customize the SDK behavior when creating the client:
$client = new JsonBinClient('YOUR_API_KEY'); $client->getConfig()->setTimeout(60);