Convert curl to Guzzle request options
This curl to Guzzle converter translates a curl command into PHP code for GuzzleHttp\Client. Rather than dumping everything into a raw body, it picks the Guzzle request option that matches your data: json for JSON payloads, form_params for URL-encoded forms, multipart for file uploads and query for the query string. The result is shorter and easier to maintain than the equivalent curl_setopt code.
Example: form post
curl -d 'name=Ann&age=31' https://api.example.com/login becomes:
$client = new Client([
'allow_redirects' => false,
]);
$response = $client->request('POST', 'https://api.example.com/login', [
'form_params' => [
'name' => 'Ann',
'age' => '31',
],
]);Because the command used a bare -d with key=value pairs, the body is decoded into a form_params array. Guzzle re-encodes it and sets the application/x-www-form-urlencoded header for you.
Option mapping
- JSON: a JSON body with a JSON Content-Type turns into a PHP array under
'json'; the duplicate Content-Type header is dropped. - Query string: parameters in the URL move to
'query', and the URL is shortened to its path. When a key repeats (?tag=a&tag=b), the full URL is kept instead. - Uploads: each
-Ffield becomes amultipartentry, with files opened byfopen($path, 'r');-T filebecomes a PUT with'body' => fopen('file', 'r'). - Auth:
-u user:passmaps to'auth' => [user, pass]. - Client options:
-msets'timeout',--connect-timeoutsets'connect_timeout',-ksets'verify' => false, and a missing-Ladds'allow_redirects' => false, because Guzzle follows redirects by default. - Compression:
--compressedadds'decode_content' => trueand anAccept-Encoding: gzip, deflateheader.
Limitations
Any other body, such as XML or text, is passed through as 'body'. Proxy flags (-x) and other unsupported options like --cert are not converted; each one appears as a comment right after the <?php tag. Guzzle also throws a ClientException or ServerException for 4xx and 5xx responses by default; set 'http_errors' => false if you want to inspect them like curl does.
Headers are written as an associative array, so you can merge them with defaults set on the client through the 'headers' constructor option, which is handy when many requests share one bearer token.
Working in Laravel? The Http facade wraps Guzzle, so the same options apply, for example Http::withBasicAuth() or Http::asForm()->post().
Privacy
The conversion runs in your browser, and tokens or passwords in the command are never uploaded. If you would rather use the native cURL extension, try curl to PHP. You can also test the endpoint first in the API tester.