cURL to PHP Converter

Paste a cURL command and get equivalent PHP using the built-in curl extension — the same library the shell command wraps, so the mapping is direct. The parser reads the method, repeated headers, request body, -u basic auth and cookies, then emits curl_init() plus a single curl_setopt_array() block with CURLOPT_RETURNTRANSFER, CURLOPT_HTTPHEADER and friends, followed by curl_exec and curl_close. Everything runs in your browser.

How to use the cURL to PHP Converter

Paste a cURL command into the cURL command field — a browser Copy as cURL string with backslash line continuations is fine. Press Convert to PHP (it also converts live as you type) and the PHP code appears below. Use Example to load a JSON POST with a bearer token so you can see how headers and the body map, then Copy the result. The output opens with <?php, calls curl_init(), then passes a single associative array to curl_setopt_array() setting CURLOPT_URL, CURLOPT_RETURNTRANSFER => true so the body is returned instead of printed, CURLOPT_CUSTOMREQUEST for the method, CURLOPT_HTTPHEADER as an array of 'Key: Value' strings, CURLOPT_POSTFIELDS for the body, and CURLOPT_USERPWD for -u credentials. It finishes with curl_exec, an HTTP-status read via curl_getinfo, and curl_close.

Converting cURL to PHP with the curl extension

The command-line curl tool and PHP’s curl_* functions are both thin wrappers over the same libcurl, so translating one to the other is mostly about mapping flags to CURLOPT_* constants. A handle from curl_init() is configured option by option — or, more cleanly, all at once with curl_setopt_array() — then executed with curl_exec() and released with curl_close(). This converter generates that exact shape so the result looks like idiomatic PHP rather than a literal transcription.

The single most important option is CURLOPT_RETURNTRANSFER => true. Without it, curl_exec() writes the response straight to output and returns only a boolean, which surprises everyone the first time. Headers are passed to CURLOPT_HTTPHEADER as a flat array of 'Name: Value' strings — not an associative array. Any Content-Length header is dropped because libcurl computes it from CURLOPT_POSTFIELDS. The method is set with CURLOPT_CUSTOMREQUEST, and -u user:pass maps to CURLOPT_USERPWD, which produces the basic Authorization header for you.

This is the focused PHP path. If you would rather assemble a request from form fields, or need the same call in another language, the cURL to Python converter and the broader multi-language cURL converter cover those cases.

Common use cases

  • API integration — drop a third-party endpoint into a PHP backend without hand-writing every curl_setopt call.
  • WordPress / Laravel glue — get a raw cURL baseline before wrapping it in wp_remote_post or Guzzle.
  • DevTools to server — turn a browser “Copy as cURL” into a server-side request.
  • Documentation — show an endpoint as native PHP next to the shell example.
  • Legacy environments — target hosts where the curl extension is available but adding Composer packages is not.
  • Learning libcurl — see which CURLOPT_* constant each cURL flag corresponds to.

Frequently asked questions

Why does the code set CURLOPT_RETURNTRANSFER?

So curl_exec() returns the response body as a string instead of echoing it to output and returning only true/false. It is the option people forget most often.

Does it use Guzzle or any package?

No. The output uses only PHP’s native curl extension, so it runs anywhere that extension is enabled with no Composer dependencies.

How are headers formatted?

CURLOPT_HTTPHEADER takes a flat array of 'Name: Value' strings rather than an associative map, and the converter builds it that way.

How is -u basic auth handled?

It maps to CURLOPT_USERPWD => 'user:pass', which makes libcurl add the correct basic Authorization header automatically.

Can I get the same request in another language?

Yes — see the cURL to Python converter or the multi-language cURL converter.