PHP code example of phpnomad / fetch
1. Go to this page and download the library: Download phpnomad/fetch 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/ */
phpnomad / fetch example snippets
namespace MyApp\Http;
use PHPNomad\Fetch\Interfaces\FetchStrategy;
use PHPNomad\Fetch\Models\FetchPayloadBuilder;
use PHPNomad\Http\Enums\Method;
class WidgetClient
{
protected FetchStrategy $fetchStrategy;
public function __construct(FetchStrategy $fetchStrategy)
{
$this->fetchStrategy = $fetchStrategy;
}
public function listWidgets(int $page, int $perPage): array
{
$response = $this->fetchStrategy->fetch(
(new FetchPayloadBuilder())
->setMethod(Method::Get)
->setUrl('https://api.example.com/widgets')
->setHeader('Accept', 'application/json')
->setParam('page', $page)
->setParam('per_page', $perPage)
->build()
);
if ($response->getStatus() >= 400) {
return [];
}
return $response->getJson();
}
}