How to use guzzle in Laravel Project

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering XML Communication in Laravel with Guzzle

As a senior developer working within the Laravel ecosystem, you often need to interact with external services. Whether these services communicate via RESTful JSON APIs or traditional formats like XML, reliable HTTP communication is paramount. When dealing with legacy systems or specific integrations, communicating via XML is common. The challenge then becomes bridging your existing PHP knowledge (like using cURL) into a modern, elegant framework like Laravel using Guzzle.

This post will guide you through the practical steps of using the Guzzle HTTP client within a Laravel application to send complex XML payloads to an external web service, directly addressing your goal of integrating your specific configuration request.

Why Guzzle for XML Communication?

Guzzle is a powerful, elegant HTTP client for PHP. While XML parsing itself requires dedicated methods (like SimpleXMLElement), Guzzle excels at the heavy lifting: managing connections, setting headers, handling POST/GET requests, and reliably receiving raw response data from endpoints. By leveraging Guzzle, you move away from manual cURL setup, leading to cleaner, more maintainable, and more readable code—a core principle we champion in modern Laravel development, as emphasized by the principles found at laravelcompany.com.

Step 1: Preparing the XML Payload

Before sending anything via Guzzle, you must construct your XML body as a string. Your provided example shows that this involves dynamically building tags and attributes based on variables ($from, $adults, etc.). This construction phase remains crucial, regardless of whether you use cURL or Guzzle.

For the sake of demonstration, we will assume you have already built the $requestXmlBody string exactly as shown in your example.

// Assume these variables are populated from your application logic
$from = '2023-10-01';
$ddays = 7;
$origin = 'JFK';
$destination = 'LAX';
// ... other variables calculated previously

$requestXmlBody  = '';
$requestXmlBody  = '<?xml version="1.0" encoding="UTF-8"?>';
$requestXmlBody .= '<FAB_PkgAvailRQ Target="test" Version="2002A" xmlns="https://localhost.com/find">';
// ... (insert all your dynamic XML segments here)
$requestXmlBody .= '<DepartureAirports><Airport>' . $origin . '</Airport></DepartureAirports>';
// ... continue building the entire structure
$requestXmlBody .= '</FAB_PkgAvailRQ>';

Step 2: Executing the Request with Guzzle

Now, we replace the manual curl_init() and curl_exec() calls with a single, expressive Guzzle call. Since you are sending a complex XML body, we will use the POST method and ensure the content type is correctly set for the server to interpret the data properly.

In a typical Laravel controller or service class, you would inject the Guzzle client (often configured in your service container) to perform this action.

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

class XmlService
{
    protected $httpClient;

    public function __construct(Client $httpClient)
    {
        $this->httpClient = $httpClient;
    }

    public function sendXmlRequest(string $xmlPayload, string $endpoint)
    {
        try {
            $response = $this->httpClient->request('POST', $endpoint, [
                'headers' => [
                    'Content-Type' => 'application/xml', // Crucial for XML services
                    // Add any necessary authentication headers here (e.g., API keys)
                ],
                'body' => $xmlPayload,
            ]);

            // Get the raw response body as a string
            $result = (string) $response->getBody();
            
            // Process the XML response using SimpleXML
            $xmlResult = simplexml_load_string($result);
            return $xmlResult;

        } catch (RequestException $e) {
            // Handle HTTP errors (4xx or 5xx responses)
            throw new \Exception("Guzzle Request Failed: " . $e->getMessage());
        } catch (\Exception $e) {
            // Handle XML parsing errors
            throw new \Exception("Error processing XML response: " . $e->getMessage());
        }
    }
}

// Example usage within a Laravel context:
$xmlPayload = /* The string built in Step 1 */;
$endpoint = 'http://localhost.com/23'; // Your target URL

try {
    $service = new XmlService(new Client());
    $xmlResponse = $service->sendXmlRequest($xmlPayload, $endpoint);
    // Now $xmlResponse is a SimpleXMLElement object ready for processing!
    dd($xmlResponse); 
} catch (\Exception $e) {
    // Log the error properly in a real application
    \Log::error("XML Communication Error: " . $e->getMessage());
}

Conclusion

Migrating from raw PHP cURL to Guzzle for XML communication simplifies your code significantly. By separating the responsibility—building the payload, setting the request options (headers and body), and handling the response—you create a system that is far more robust and easier to test. Remember that while Guzzle handles the HTTP transport flawlessly, the final step of interpreting the XML response still requires standard PHP XML libraries like SimpleXMLElement. Embrace these tools to build scalable and maintainable applications within Laravel!