PHP code example of unifreak / fetcher

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

    

unifreak / fetcher example snippets



// register facade
if (!class_exists('Fetcher')) {
    class_alias(Unifreak\Fetcher\FetcherFacade::class, 'Fetcher');
}

// register service provider
$app->register(Unifreak\Fetcher\FetcherServiceProvider::class);


/**
 * Api configuration
 * Better keep those configurations in a seperated config files
 */
$api = [
    'url' => 'http://example.com/demo/api', // Api url, **NOTE without any query parameter**
    'method' => 'POST', // Api request method, default to 'GET'
    'timeout' => 0.01, // Api request timeout seconds
    'codeField' => 'payload.code', // Response code field, can nest with `.`, default to 'code'
    'dataField' => 'payload.data', // Response data field, can nest with `.`, default to 'data'
    'messageField' => 'payload.msg', // Response message field, can nest with `.`, default to 'message'
    'successCode' => 2, // When response code equal to this configured value, Fetcher considers api call success. default to 1
    'signer' => DemoSigner::class, // Signer class, see below
];

/**
 * Assume you have registered facade, you can use `Fetcher::fetch()` to do api calls
 * Otherwise, you need to create Fetcher instance manually like: `new Fetcher(new GuzzleHttp\Client());`
 *
 * fetch() accepts the following parameters:
 * 1. 


namespace App\Signers;

use Unifreak\Fetcher\Signer\Signer;

class DemoSigner implements Signer
{
    /**
     * `Signer` must implement a `sign()` method. `sign()` method do the real signing logic
     */
    public function sign(array $payload) {
        return $payload + ['_sn' => 'abcdefg'];
    }
}