Skip to content
ZeroServer.tools

curl to PHP

Convert curl commands to PHP code using cURL extension, Guzzle, or file_get_contents streams.

Style: cURL (ext)Method: POSTHeaders: 2Input Lines: 4Output Lines: 21Output Bytes: 548

Looks like this contains credentials. They are converted in this tab only — not uploaded, and not included in the link that Share or Copy link produces.

curl to PHP cURL & Guzzle

PHP offers built-in cURL options via curl_setopt_array() as well as modern HTTP clients like Guzzle. This converter automatically structures headers, request body payloads, HTTP Basic credentials, and SSL verification settings into clean PHP code.

Built and maintained by Meet Shah · Last updated

What this tool is used for

  • Converting an API's curl example into PHP cURL code.
  • Reproducing a captured request inside an existing PHP client.
  • Getting the option constants right without reading the whole manual.
  • Producing a snippet for a one-off script.
  • Checking how auth and cookies are expressed as options.

Frequently Asked Questions

Why is the response printed instead of returned?
CURLOPT_RETURNTRANSFER defaults to false, so curl_exec() writes the body straight to output and returns only true. Set curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) to capture it as a string. This is the classic PHP cURL gotcha.
Should I use the cURL extension or Guzzle?
The ext-curl bindings map one-to-one onto curl flags, so a port is mechanical. Guzzle wraps them with PSR-7 messages, middleware and async promises, and is the norm in Laravel/Symfony projects. Guzzle still uses ext-curl underneath when available.
How do I send a JSON body?
CURLOPT_POSTFIELDS with json_encode($data), plus CURLOPT_HTTPHEADER including 'Content-Type: application/json'. If you pass an array to CURLOPT_POSTFIELDS instead, PHP sends multipart/form-data — not form-urlencoded — which surprises most people.
How do I see why a request failed?
curl_exec() returns false on failure; call curl_error($ch) and curl_errno($ch) for the reason, and curl_getinfo($ch, CURLINFO_HTTP_CODE) for the status. A 404 is NOT a failure here — curl_exec() succeeds and you must check the code yourself.
Do I need to close the handle?
Call curl_close($ch) when done. Since PHP 8.0 the handle is an object collected automatically, so this is no longer strictly required, but it remains good practice and is mandatory on PHP 7.x to free the resource promptly.
Why does a request fail with an SSL certificate error on Windows?
Because PHP ships no CA bundle of its own and Windows does not supply one in the format cURL expects. Point `curl.cainfo` in php.ini at a downloaded `cacert.pem` — disabling `CURLOPT_SSL_VERIFYPEER` silences the error and removes the protection.

Common errors and gotchas

  • Forgetting `CURLOPT_RETURNTRANSFER`, without which the response is printed rather than returned.
  • Disabling `CURLOPT_SSL_VERIFYPEER` to make an error go away, which removes TLS verification.
  • Setting `CURLOPT_POSTFIELDS` to an array, which switches to multipart rather than the JSON you meant.
  • Not calling `curl_close`, which on a long-running process leaks handles.
  • Assuming a non-2xx sets an error, when you must read `CURLINFO_HTTP_CODE` yourself.

Related Developer Utilities tools

Private & free — this tool runs entirely in your browser.

IndieKitShip your Next.js startup in days.affiliate