cURL to PHP Converter

Paste the curl command from an API document and get PHP code that sends the same request.

curl command
PHP
Runs locally in your browser

About the cURL to PHP Converter

API documentation shows requests as curl one liners, and PHP developers end up translating them by hand. This converter tokenises the command the way a shell would, so single quotes, double quotes, backslash line continuations and escaped characters are all read correctly before any code is generated.

The flags that change a request are understood: -X for the method, repeated -H headers, -d and its data variants joined with ampersands, --json, multipart -F fields including @file uploads, basic auth from -u, cookies, user agent and referer shortcuts, plus -L, -k and --compressed. Noise such as -s or -v is ignored because it only affects terminal output. A missing scheme is filled in as https.

Three output styles cover the common stacks. The cURL extension version uses curl_setopt_array and checks curl_error before returning. The Guzzle version emits a request() call with headers, body and auth options. The streams version builds a context for file_get_contents when the project cannot rely on the cURL extension being installed.

Read the tokens of a URL first with the URL Parser, or turn a JSON body into a native array with JSON to PHP Array when you would rather build the payload in code than paste a string.

How to use

  1. Copy the whole curl command, including any backslash line breaks.
  2. Paste it into the left pane.
  3. Pick cURL extension, Guzzle or file_get_contents.
  4. Copy the PHP into your project and replace the credentials.

Common questions

Does it keep my API key?
The key is copied into the generated code on your machine only. Nothing is transmitted, though you should still swap it for an environment variable before committing.
Which PHP version does the output target?
The cURL and streams output runs on PHP 7.0 and later. The Guzzle output expects Guzzle 7 installed through Composer.
Are file uploads handled?
Yes. A -F field written as name=@/path/file becomes a CURLFile instance in the generated array.
What if the command has no method flag?
It defaults to GET, or to POST when the command carries data or form fields, which matches how curl itself behaves.