PHP code example of antonioprimera / laravel-api-client

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

    

antonioprimera / laravel-api-client example snippets


    use AntonioPrimera\ApiClient\ApiClient;
    use AntonioPrimera\ApiClient\Clients\HttpClient;
    
    $response = ApiClient::makeSanctumClient()
        ->withToken('some-token')
        ->post('http://my-api-endpoint-url.com', ['id' => 15]);


    use AntonioPrimera\ApiClient\ApiClient;
    use AntonioPrimera\ApiClient\Clients\HttpClient;

    $response = ApiClient::makeHttpClient()
        ->withAuthenticationType(HttpClient::AUTHENTICATION_TYPE_BASIC)
        ->withCredentials(['username' => 'my-user-name', 'password' => 'my-password'])
        ->get('http://my-api-endpoint-url.com', ['id' => 15]);

    use AntonioPrimera\ApiClient\ApiClient;
    use AntonioPrimera\ApiClient\Clients\HttpClient;

    $response = ApiClient::makeHttpClient()
        ->withAuthenticationType(HttpClient::AUTHENTICATION_TYPE_QUERY)
        ->withCredentials(['user' => 'my-user-name', 'pass' => 'my-pass', 'tk' => 'my-token'])
        ->get('http://my-api-endpoint-url.com', ['id' => 15]);

    use AntonioPrimera\ApiClient\ApiClient;
    use AntonioPrimera\ApiClient\Clients\HttpClient;
    
    $response = ApiClient::getClient('mySanctumClient')
        ->callEndpoint('setTracks', ['tracks' => '...']);

    use AntonioPrimera\ApiClient\ApiClient;
    use AntonioPrimera\ApiClient\Clients\HttpClient;
 
    $response = ApiClient::makeClient('myBasicHttpClientWithCredentials')
        ->callEndpoint('getUser', ['user-id' => 15]);


    use AntonioPrimera\ApiClient\ApiClient;
    use AntonioPrimera\ApiClient\Clients\HttpClient;
    
    $response = ApiClient::getClient('myQueryHttpClient')
        ->callEndpoint('getMenu');
        

    use AntonioPrimera\ApiClient\ApiClient;
    use AntonioPrimera\ApiClient\Clients\HttpClient;
    
    $response = ApiClient::getClient('myQueryHttpClient')
        ->post('http://my-api-endpoint-url.com', ['user' => ['id' => 15, 'name' => 'Gigi']]);

use AntonioPrimera\ApiClient\ApiClient;

class MyApiClient extends ApiClient
{
    protected static $config = 'myApiConfig';
}


//sample config
return [

    //the name of the client, usually the name of an external api provider e.g. "github" / "instagram"
    'mySanctumClient' => [
        
        //mandatory to have at least the authentication type provided
        'authentication' => [
            'type'  => 'sanctum',
            
            //(optional) if not provided, must be provided at run-time
            'token' => env('MY_SANCTUM_TOKEN'),
        ],
        
        //(optional) if rootUrl is provided it will be prepended to each endpoint url
        'rootUrl' => 'https://localhost:8080/',
        
        //the list of all endpoints
        'endpoints' => [
        
            'setTracks' => [                //endpoint name, to be used in development (like a route name)
                'url'    => '/tracks/',     //url is mandatory
                'method' => 'post',         //(optional) by default: 'get'
            ],
            
            //endpoints with method 'get' can also be provided as strings
            'getPositions' => 'positions',
        ],
    ],
    
    //example of a provider with a basic http authentication
    'myBasicHttpClientWithCredentials' => [
        'authentication' => [
            'type' => 'http:basic',
            
            //optional
            'credentials' => [
                'username' => env('MY_HTTP_CLIENT_USERNAME'),
                'password' => env('MY_HTTP_CLIENT_PASSWORD'),
            ],
        ],
        
        'endpoints' => [
            //... all endpoints are the same, regardless of the authentication type
        ],
    ],
    
    //example of a provider with an authentication via query parameters (credentials are sent as part of the url)
    'myQueryHttpClient' => [
        'authentication' => [
            'type' => 'http:query',
            
            //credentials are optional (can be provided at runtime via method $client->setCredentials(...)
            'credentials' => [
                'key' 		 => 'my-key',
                'passphrase' => 'my-phrase',
                'token'		 => 'my-token',
            ],

        ],
        
        'endpoints' => [
            //... all endpoints are the same, regardless of the authentication type
        ],
    ],    
];