PHP code example of masnathan / api-caller

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

    

masnathan / api-caller example snippets


use MASNathan\APICaller\Client;
use MASNathan\APICaller\Caller;

class MyPackageClient extends Client
{
    /**
     * Here you can set the default headers and parameters on a global scope
     */
    public function __construct($ip = null)
    {
        $this->setDefaultHeaders([
            'User-Agent' => 'PHP APICaller SDK',
            'Accept'     => 'application/json',
            'Token'      => '123456',
        ]);
        $this->setDefaultParameters([
            'ip' => $ip ?: '127.0.0.1',
        ]);
    }

    /**
     * Returns the API Endpoint
     *
     * @return string
     */
    public function getEndpoint()
    {
        return 'http://api.domain.com/v1/';
    }
}

class MyPackageCaller extends Caller
{
    public function requestSomething($foo, $bar)
    {
        $params = [
            'foo' => $foo,
            'bar' => $bar,
        ];

        // this will result in this url http://api.domain.com/v1/some-method.json?ip={$ip}&foo={$foo}&bar={$bar}
        $response = $this->client->get('some-method.json', $params);

        $data = $this->handleResponseContent($response, 'json');

        // Do something with your data

        return $data;
    }
}

$client = new MyPackageClient('8.8.8.8');
$caller = new MyPackageCaller($client);

$result = $caller->requestSomething(13, 37);

var_dump($result);