How to make a simple HTTP GET request using Guzzle in PHP?

To make a simple HTTP GET request using Guzzle in PHP, follow these steps:

  1. Install Guzzle via Composer by running the following command in your project directory:

    composer require guzzlehttp/guzzle
  2. Import the Guzzle library at the top of your PHP file:

    use GuzzleHttp\Client;
  3. Create a new Guzzle client instance:

    $client = new Client();
  4. Use the client to send a GET request to your desired URL:

    $response = $client->get('https://example.com');
  5. 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.