Guzzle returns cURL error 3:malformed
When you encounter cURL error 3: <url> malformed while using Guzzle, the immediate instinct is often to check your network connectivity or firewall settings. However, in the specific context of the code provided in the source material, the root cause is almost never a network issue. It is a configuration mismatch within the client itself.
The error occurs because the GuzzleHttp\Client constructor defaults to a relative base URL if you do not explicitly provide one. In your example:
$client = new Client();
$client->get('/', ['verify' => true]);
The first line creates a client with no base URL defined. When you subsequently call $client->get('/'), Guzzle attempts to construct the full request URI by prepending this empty string to the path. The result is effectively an empty string or just / depending on how the internal logic resolves it, which cURL interprets as a malformed URL because it expects a scheme (like http://) and a host.
The second line in your snippet attempts to fix this:
$client->get('https://api.github.com/');
While this specific call might succeed depending on Guzzle's internal fallback behaviors, relying on implicit defaults is fragile. The error message shown in the screenshot often points to the first request failing before the second one even executes, or the client state being confused by the initial relative path attempt.
Explicitly Setting the Base URL
To resolve this immediately, you must instantiate the Client with a valid base URL. This tells Guzzle exactly where to send requests if the paths provided in subsequent calls are relative.
Update your code as follows:
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => 'https://api.github.com/',
]);
// Now this works because it resolves to https://api.github.com/users
$response = $client->get('/users');
// Or you can keep your original call, but it now has a valid context
$response = $client->get('https://api.github.com/');
By passing an associative array as the first argument to Client, you configure the transport layer correctly. The base_uri key ensures that every subsequent request has a complete scheme and host.
Handling SSL Verification
You also included 'verify' => true in your original code. This is actually unnecessary for most public APIs like GitHub, but it serves as a good reminder of Guzzle's security defaults.
By default, Guzzle sets verify to true. This instructs the underlying cURL handler to validate the SSL certificate against the trusted system bundle. While this prevents man-in-the-middle attacks, it can cause issues in local development environments (like WAMP) where self-signed certificates are often used.
If you are testing locally and encounter SSL errors, you might see a different error regarding the certificate. In those cases, you would set 'verify' => false temporarily for debugging, but never in production code. For the specific error 3 issue, removing the verify option or keeping it default is fine; the malformed URL is the blocker here.
Why Relative URLs Fail Without a Base URI
It is important to understand that HTTP clients distinguish between absolute and relative URIs. An absolute URI contains the scheme and host (e.g., https://example.com/path). A relative URI only contains the path or fragment.
When you call $client->get('/') without a base_uri, Guzzle tries to interpret / as an absolute request string because it lacks context. cURL rejects this because it cannot determine which server to contact. This is why explicit configuration is required when using the client class directly, rather than relying on implicit defaults that vary between Guzzle versions.
If you are working within a Laravel application, consider injecting the Client into your controller or using Laravel's built-in HTTP facade (Http::get) if you prefer not to manage the underlying Guzzle instance manually. However, understanding how the base URI works allows for more granular control over timeouts, headers, and middleware when building custom clients.
