Guzzle (HTTP Client)
The artiframe add guzzle command installs the guzzlehttp/guzzle package, which makes sending requests to external APIs (REST, SOAP, GraphQL) incredibly easy in PHP.
It frees you from the long and confusing syntax of native cURL functions, offering a modern and fluent approach.
💡 Information: Direct Usage
ArtiFrame does not copy a specific integration (Wrapper/Stub) class to the
ArtiFrame does not copy a specific integration (Wrapper/Stub) class to the
src/Service folder for this package. The installation simply includes the package into the project via Composer.
The codes below are basic usage examples demonstrating the package's native operations. For more information, please review the Guzzle Documentation.
Installation
terminal
$ artiframe add guzzle
Usage Example
ApiController.php
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
// 1. Create the Client
$client = new Client([
'base_uri' => 'https://jsonplaceholder.typicode.com/',
'timeout' => 5.0, // Throws an Exception if no response within 5 seconds
]);
try {
// 2. Making a GET Request
$response = $client->request('GET', 'users');
// HTTP Status Code (e.g., 200)
echo "Status Code: " . $response->getStatusCode() . "<br>";
// Convert JSON response to an Array
$users = json_decode($response->getBody(), true);
echo "First User: " . $users[0]['name'] . "<hr>";
// 3. POST Request (Sending JSON Body and Headers)
$postResponse = $client->request('POST', 'posts', [
'headers' => [
'Authorization' => 'Bearer YOUR_API_TOKEN',
'Accept' => 'application/json'
],
'json' => [
'title' => 'Guzzle is Awesome!',
'body' => 'Testing via ArtiFrame.',
'userId' => 1
]
]);
echo "POST Successful! Response: <br>";
echo $postResponse->getBody();
} catch (RequestException $e) {
// Falls here if the server returns a 404 or 500
echo "API Error: " . $e->getMessage();
}