Guzzle is a popular HTTP client library in PHP, and it can be used in Symfony to make HTTP requests. Here are some examples on how to use Guzzle HTTP in Symfony.
Installing Guzzle
Add Guzzle as a dependency to your Symfony project using Composer
composer require guzzlehttp/guzzle
Importing Guzzle
In your Symfony controller or service file, import Guzzle using the namespace
use GuzzleHttp\Client;
Creating a Guzzle client
Instantiate a Guzzle client in your controller or service
$client = new Client();
Sending a GET request
To send a GET request using Guzzle, you can use the get()
method
$response = $client->get('https://api.example.com/posts');
$statusCode = $response->getStatusCode();
$body = $response->getBody()->getContents();
Sending a POST request
To send a POST request using Guzzle, you can use the post()
method
$response = $client->post('https://api.example.com/posts', [
'form_params' => [
'title' => 'New Post',
'content' => 'Lorem ipsum dolor sit amet.'
]
]);
$statusCode = $response->getStatusCode();
$body = $response->getBody()->getContents();
Sending request headers in the call
You can include headers in your Guzzle requests by passing them as an array
$response = $client->get('https://api.example.com/posts', [
'headers' => [
'Authorization' => 'Bearer your_token',
'Accept' => 'application/json'
]
]);
Handling exceptions
Guzzle may throw exceptions for various errors. You can catch these exceptions and handle them appropriately
try {
$response = $client->get('https://api.example.com/posts');
// Process the response
} catch (Exception $e) {
// Handle the exception
}
Posting a file to an Url
Prepare the file to be sent
$file = '/path/to/file.png'; // Replace with the actual file path
$fileName = 'file.png'; // Replace with the desired filename
Send a POST request with the file
$response = $client->post('https://api.example.com/upload', [
RequestOptions::MULTIPART => [
[
'name' => 'file',
'contents' => fopen($file, 'r'),
'filename' => $fileName
]
]
]);
In the above example, we use the RequestOptions::MULTIPART
option to indicate that we are sending multipart form data, which is suitable for file uploads. We create an array specifying the file details, including the field name, file contents (opened with fopen()
), and the desired filename.
Handle the response as needed
$statusCode = $response->getStatusCode();
$body = $response->getBody()->getContents();
You can retrieve the status code and the response body using the appropriate methods provided by Guzzle.
Remember to replace 'https://api.example.com/upload'
with the actual URL where you want to upload the file. Additionally, ensure that the server-side endpoint is configured to handle file uploads correctly.
This example demonstrates how to send a single file. If you want to send multiple files, you can simply add more items to the MULTIPART array, each representing a file to be uploaded.
Make sure to handle any exceptions that may be thrown by Guzzle, as shown in the previous example, to ensure proper error handling.
Get a file from an Url
Send a GET request to retrieve the file
$url = 'https://example.com/path/to/file.pdf'; // Replace with the URL of the file
$response = $client->get($url);
In the above example, replace 'https://example.com/path/to/file.pdf'
with the actual URL of the file you want to retrieve.
Retrieve the contents of the file from the response
$fileContents = $response->getBody()->getContents();
The $fileContents
variable now contains the contents of the file obtained from the response.
Optionally, you can save the file to the local filesystem
$filePath = '/path/to/save/file.pdf'; // Replace with the desired file path
file_put_contents($filePath, $fileContents);
Using file_put_contents()
, you can save the contents of the file to the specified path. Make sure the directory is writable by the PHP process.
For more information go to the Guzzle Http Documentation