PHP code example of imamnc / efihub-client

1. Go to this page and download the library: Download imamnc/efihub-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

imamnc / efihub-client example snippets


use Efihub\Facades\Efihub;

// GET request
$response = Efihub::get('/users');

// POST request
$response = Efihub::post('/users', [
    'name' => 'John Doe',
    'email' => '[email protected]'
]);

// PUT request
$response = Efihub::put('/users/1', [
    'name' => 'Jane Doe'
]);

// DELETE request
$response = Efihub::delete('/users/1');

use Efihub\EfihubClient;

class UserService
{
    public function __construct(private EfihubClient $efihub)
    {
    }

    public function getAllUsers()
    {
        $response = $this->efihub->get('/users');

        if ($response->successful()) {
            return $response->json();
        }

        throw new Exception('Failed to fetch users');
    }
}

$response = Efihub::get('/users');

// Check if successful
if ($response->successful()) {
    $data = $response->json();
    $status = $response->status();
}

// Handle errors
if ($response->failed()) {
    $error = $response->json();
    Log::error('EFIHUB API Error', $error);
}

return [
    'client_id' => env('EFIHUB_CLIENT_ID'),
    'client_secret' => env('EFIHUB_CLIENT_SECRET'),
    'token_url' => env('EFIHUB_TOKEN_URL', 'https://efihub.morefurniture.id/oauth/token'),
    'api_base_url' => env('EFIHUB_API_URL', 'https://efihub.morefurniture.id/api'),
];



namespace App\Services;

use Efihub\Facades\Efihub;
use Illuminate\Support\Facades\Log;

class EfihubService
{
    public function createUser(array $userData): array
    {
        try {
            $response = Efihub::post('/users', $userData);

            if ($response->successful()) {
                return $response->json();
            }

            throw new \Exception('Failed to create user: ' . $response->body());

        } catch (\Exception $e) {
            Log::error('EFIHUB Create User Error', [
                'error' => $e->getMessage(),
                'data' => $userData
            ]);

            throw $e;
        }
    }

    public function getUsers(array $filters = []): array
    {
        $response = Efihub::get('/users', $filters);

        return $response->successful()
            ? $response->json()
            : [];
    }
}

use Illuminate\Support\Facades\Http;

Http::fake([
    'efihub.morefurniture.id/oauth/token' => Http::response([
        'access_token' => 'fake-token',
        'expires_in' => 3600
    ]),
    'efihub.morefurniture.id/api/*' => Http::response([
        'data' => ['users' => []]
    ])
]);
bash
php artisan vendor:publish --provider="Efihub\EfihubServiceProvider" --tag=config