To make a simple HTTP GET request using Guzzle in PHP, follow these steps:
Install Guzzle via Composer by running the following command in your project directory:
composer require guzzlehttp/guzzle
Import the Guzzle library at the top of your PHP file:
use GuzzleHttp\Client;
Create a new Guzzle client instance:
$client = new Client();
Use the client to send a GET request to your desired URL:
$response = $client->get('https://example.com');
Retrieve the response body as a string:
$body = $response->getBody()->getContents();
You can also retrieve the response status code, headers, etc. as needed using the $response
object.
Here's a complete example:
use GuzzleHttp\Client;
// Create a new Guzzle client instance
$client = new Client();
// Send a GET request to a URL
$response = $client->get('https://example.com');
// Retrieve the response body as a string
$body = $response->getBody()->getContents();
// Output the response
echo $body;
Remember to replace 'https://example.com'
with your desired URL.