1. Go to this page and download the library: Download harmonyio/http-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/ */
harmonyio / http-client example snippets
declare(strict_types=1);
namespace Foo;
use Amp\Artax\DefaultClient;
use Amp\Redis\Client;
use HarmonyIO\Cache\Provider\Redis;
use HarmonyIO\Cache\Ttl;
use HarmonyIO\HttpClient\Client\ArtaxClient;
use HarmonyIO\HttpClient\Message\CachingRequest;
use function Amp\wait;
// create instance of the HTTP client
$httpClient = new ArtaxClient(
new DefaultClient(),
new Redis(new Client('tcp://127.0.0.1:6379'))
);
// create a new request to be cached for 10 seconds
$request = new CachingRequest('TestRequestKey', new Ttl(10), 'https://httpbin.org/get');
// make the request and cache it
$result = wait($httpClient->request($request));
// all consecutive requests will now used the cached result instead of calling the external service again
$result = wait($httpClient->request($request));
declare(strict_types=1);
namespace Foo;
use Amp\Artax\DefaultClient;
use Amp\Redis\Client;
use HarmonyIO\Cache\Provider\Redis;
use HarmonyIO\HttpClient\Client\ArtaxClient;
use HarmonyIO\HttpClient\Message\Request;
use function Amp\wait;
// create instance of the HTTP client
$httpClient = new ArtaxClient(
new DefaultClient(),
new Redis(new Client('tcp://127.0.0.1:6379'))
);
// create a new request to be cached for 10 seconds
$request = new Request('https://httpbin.org/get');
// make the request (the results will NOT be cache)
$result = wait($httpClient->request($request));
// make the same request again
$result = wait($httpClient->request($request));
declare(strict_types=1);
namespace Foo;
use HarmonyIO\HttpClient\Message\Request;
$request = (new Request('https://httpbin.org/post', 'POST'))
->setProtocolVersions('1.1', '2.0')
->addHeader('foo', 'bar')
->addHeader('baz', 'qux')
->setBody('foobar')
;
declare(strict_types=1);
namespace Foo;
use HarmonyIO\Cache\Ttl;
use HarmonyIO\HttpClient\Message\CachingRequest;
$request = (new CachingRequest('UniqueCachingKey', new Ttl(Ttl::ONE_HOUR), 'https://httpbin.org/get'))
->setProtocolVersions('1.1', '2.0')
->addHeader('foo', 'bar')
->addHeader('baz', 'qux')
;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.