1. Go to this page and download the library: Download larafony/http-guzzle 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/ */
larafony / http-guzzle example snippets
use Larafony\Http\Guzzle\ServiceProviders\GuzzleServiceProvider;
$app->withServiceProviders([
GuzzleServiceProvider::class
]);
use Larafony\Framework\Validation\Attributes\Email;
use Larafony\Framework\Validation\Attributes\IsValidated;
use Larafony\Framework\Validation\Attributes\MinLength;
use Larafony\Framework\Validation\FormRequest;
use Larafony\Framework\Web\Config;
use Larafony\Http\Guzzle\GuzzleHttpClient;
use Larafony\Framework\Web\Application;
use Larafony\Framework\Web\Controller;
use Larafony\Framework\Routing\Advanced\Attributes\Route;
use Larafony\Framework\Http\Factories\ResponseFactory;
use Larafony\Http\Guzzle\GuzzleHttpClientFactory;
use Psr\Http\Message\ResponseInterface;
final class UserRequest extends FormRequest
{
//properties are automatically injected from the request
#[IsValidated]
#[MinLength(3)]
public protected(set) string $firstName;
#[IsValidated]
#[MinLength(3)]
public protected(set) string $lastName;
#[IsValidated]
#[Email]
public protected(set) string $email;
/**
* @return array<string, string>
*/
public function toArray(): array
{
return [
'firstName' => $this->firstName,
'lastName' => $this->lastName,
'email' => $this->email,
];
}
}
final class ApiController extends Controller
{
#[Route('/fetch-users')]
public function fetchUsers(GuzzleHttpClient $client): \Psr\Http\Message\ResponseInterface
{
// Simple GET request (use guzzle client directly)
$response = $client->getGuzzle()->get('https://api.example.com/users') |> $this->decodeResponse(...);
return new ResponseFactory()->createJsonResponse($response);
}
#[Route('/update-users', methods: ['PUT', 'POST'])]
public function updateUsers(GuzzleHttpClient $client, UserRequest $request): ResponseInterface
{
//use bridge facade
$response = $client->post('https://api.example.com/users', [
'json' => $request->toArray(),
]) |> $this->decodeResponse(...);
return new ResponseFactory()->createJsonResponse($response);
}
#[Route('/authorized')]
public function authorized(): ResponseInterface
{
$token = Config::get('api.token');
$client = GuzzleHttpClientFactory::withBearerToken($token);
$response = $client->get('https://api.example.com/data') |> $this->decodeResponse(...);
return new ResponseFactory()->createJsonResponse($response);
}
private function decodeResponse (ResponseInterface $response): array
{
return json_decode($response->getBody()->getContents(), associative: true, flags: JSON_THROW_ON_ERROR);
}
}
use Psr\Http\Client\ClientInterface;
// Works with any PSR-18 compatible code
function fetchData(ClientInterface $client): array
{
$response = $client->sendRequest($request);
return json_decode($response->getBody()->getContents(), true);
}