PHP code example of allegro / toper

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

    

allegro / toper example snippets




use Toper\GuzzleClientFactory;
use Toper\StaticHostPoolProvider;

$hostPollProvider = new StaticHostPoolProvider(
    array("http://service1.com", "http://service2.com")
);
$guzzleClientFactory = new GuzzleClientFactory();

$toper = new \Toper\Client($hostPollProvider, $guzzleClientFactory);
$response = $toper->get('/')
    ->addHeader('cache-control', 'no-cache')
    ->send();

if($response->getStatusCode() == 200) {
    echo $response->getBody();
}



class MyHostPoolProvider implements \Toper\HostPoolProviderInterface
{
    /**
     * @return \Toper\HostPoolInterface
     */
    public function get()
    {
        //fetch hosts code
    }
}

$storage = new \Toper\Storage\FileStorage(
    "/tmp/"
);

$cacheLifeTime = 5; //time in seconds

$cachedHostProvider = new \Toper\CachedHostPoolProvider(
    new MyHostPoolProvider(), $storage, new \Toper\Clock(), $cacheLifeTime

sh
#install composer
curl -sS https://getcomposer.org/installer | php