PHP code example of beriyack / apiclient

1. Go to this page and download the library: Download beriyack/apiclient 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/ */

    

beriyack / apiclient example snippets



Beriyack\Client\ApiClient;

use Beriyack\Client\ApiClient;

try {
    $client = new ApiClient('https://jsonplaceholder.typicode.com');
    
    // Récupérer des données (GET)
    $post = $client->get('/posts/1');
    print_r($post);

} catch (Exception $e) {
    echo "Erreur : " . $e->getMessage();
}

use Beriyack\Client\ApiClient;

try {
    $client = new ApiClient('https://jsonplaceholder.typicode.com');

    // Créer une ressource (POST)
    $newPostData = [
        'title' => 'Mon Nouveau Titre',
        'body' => 'Contenu de mon nouveau post.',
        'userId' => 1
    ];
    $createdPost = $client->post('/posts', $newPostData);
    echo "Post créé :\n";
    print_r($createdPost);

    // Mettre à jour une ressource (PUT)
    $updatedPostData = ['title' => 'Titre Mis à Jour'];
    $updatedPost = $client->put('/posts/1', $updatedPostData);
    echo "\nPost mis à jour :\n";
    print_r($updatedPost);
} catch (Exception $e) {
    echo "Erreur : " . $e->getMessage();
}

use Beriyack\Client\ApiClient;

try {
    $client = new ApiClient('https://jsonplaceholder.typicode.com');
    $response = $client->delete('/posts/1');
    
    // Une réponse vide ou un objet vide indique généralement un succès
    echo "Ressource supprimée avec succès.";
    print_r($response);

} catch (Exception $e) {
    echo "Erreur : " . $e->getMessage();
}

use Beriyack\Client\ApiClient;

try {
    $headers = [
        'Authorization' => 'Bearer VOTRE_TOKEN_SECRET',
        'Accept'        => 'application/json',
    ];

    // Exemple pour un environnement de dev local qui nécessite un certificat spécifique
    $curlOptions = [
        CURLOPT_CAINFO => __DIR__ . '/path/to/your/cacert.pem',
        CURLOPT_TIMEOUT => 15, // Timeout de 15 secondes pour chaque requête
    ];

    $client = new ApiClient('https://api.exemple.com/v2', $headers, $curlOptions);

    // Chaque requête utilisera automatiquement le token et les options cURL
    $userData = $client->get('/user');
    print_r($userData);
} catch (Exception $e) {
    echo "Erreur : " . $e->getMessage();
}

use Beriyack\Client\ApiClient;

try {
    // Ce client est utilisé pour obtenir un token OAuth
    $authClient = new ApiClient('https://oauth.service.com');

    $data = [
        'grant_type'    => 'client_credentials',
        'client_id'     => 'YOUR_CLIENT_ID',
        'client_secret' => 'YOUR_CLIENT_SECRET'
    ];

    $headers = [
        'Content-Type' => 'application/x-www-form-urlencoded'
    ];

    $tokenResponse = $authClient->post('/token', $data, [], $headers);
    print_r($tokenResponse);
} catch (Exception $e) {
    echo "Erreur : " . $e->getMessage();
}