FormatterHQ

cURL to PHP Converter

Paste a curl command and get equivalent PHP code using the curl extension, with every option mapped to the right curl_setopt call.

Input SHELL
Output PHP
Ready.

How to use the cURL to PHP Converter

  1. Copy a curl command, for example from API documentation or your browser’s network panel (Copy as cURL).
  2. Paste it into the input editor; multi-line commands with backslashes are fine.
  3. The PHP code is generated immediately.
  4. Copy the code into your project and replace placeholder tokens with real values from configuration.

Turn curl commands into working PHP

API documentation almost always shows requests as curl commands, and browser developer tools can copy any request "as cURL". Translating those flags into PHP's curl_setopt calls by hand means looking up constant names every time. This curl to PHP converter parses the command and writes the equivalent PHP using the built-in curl extension, with no dependencies.

Example

curl -X POST 'https://api.example.com/v1/orders?notify=true' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_API_TOKEN' \
  -d '{"sku":"LAMP-01","qty":2}' \
  --max-time 30
<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.example.com/v1/orders?notify=true');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"sku":"LAMP-01","qty":2}');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Authorization: Bearer YOUR_API_TOKEN',
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
} else {
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo $response;
}

curl_close($ch);

How options map to PHP

  • -X PUT → CURLOPT_CUSTOMREQUEST; a POST with a body uses CURLOPT_POST; -I uses CURLOPT_NOBODY.
  • Headers → CURLOPT_HTTPHEADER; cookies → CURLOPT_COOKIE.
  • -d → CURLOPT_POSTFIELDS as a string; -F → an array, with @file uploads converted to new CURLFile(…).
  • -u user:pass → CURLOPT_USERPWD; --compressed → CURLOPT_ENCODING.
  • -L → CURLOPT_FOLLOWLOCATION; -m and --connect-timeout → timeout options; -x → CURLOPT_PROXY.
  • -k → disables peer and host verification. Use it only for local testing; never ship it to production, because it makes HTTPS connections vulnerable to interception.

Supported curl options

curl optionMeaning
-X, --requestHTTP method (GET, POST, PUT, PATCH, DELETE…)
-H, --headerRequest headers
-d, --data, --data-raw, --data-binary, --data-urlencodeRequest body; implies POST
--jsonJSON body with Content-Type and Accept headers
-F, --formMultipart form fields and file uploads
-u, --user and --oauth2-bearerBasic and bearer authentication
-b, --cookie, -A, -eCookies, user agent and referer
-G, --getSend -d data as query string
-L, -k, -m, -x, -I, -TFollow redirects, skip TLS checks, timeout, proxy, HEAD request, upload file

Line continuations (\), single and double quotes are parsed like a Unix shell. Options that only affect curl's own output, such as -s, -v or -o, are ignored silently; any other unsupported option is listed as a comment at the top of the generated code, so nothing is dropped without notice.

Where to get curl commands

Besides API documentation, the quickest source is your browser. Open the developer tools, switch to the Network tab, right-click a request and choose "Copy as cURL". The command includes every header the browser sent, including cookies and session tokens, so remove the ones you do not need before converting and never share them.

Making the code production-ready

  • Move tokens and passwords into environment variables instead of hard-coding them.
  • Check $status and handle 4xx/5xx responses, not just transport errors.
  • Decode JSON responses with json_decode($response, true) and handle failures.
  • In a framework, consider its HTTP client: Laravel's Http facade and Symfony HttpClient wrap the same concepts with less code.

Other languages: cURL to Python and cURL to JavaScript. To try a request directly from the browser, use the API tester. The command is parsed locally, so tokens in it are never sent to this site.

Frequently asked questions

Does the generated PHP need any libraries?
No. It uses PHP’s built-in curl extension, which is enabled on almost every PHP installation. If curl_init is undefined, enable the curl extension in php.ini.
How are file uploads with -F converted?
Fields like -F "file=@photo.jpg" become a CURLFile object in the CURLOPT_POSTFIELDS array, which PHP sends as multipart/form-data.
What happens to curl options that are not supported?
They are listed in a comment at the top of the generated code, so you can handle them manually. Output-only options such as -s and -v are ignored.
Is it safe to paste commands that contain API keys?
The conversion runs entirely in your browser, so the command is not transmitted. Still, replace real secrets with placeholders before sharing the generated code.
Why does my request not follow redirects?
Like curl, PHP does not follow redirects by default. Add -L to the curl command, or set CURLOPT_FOLLOWLOCATION to true in the code.

Last updated October 3, 2026

Guides

Related tools

HTML to PHP Converter Convert HTML into PHP echo statements, a string variable or a heredoc block with every quote and dollar sign safely escaped. JSON to PHP Array Converter Convert JSON to a PHP array literal with short [] or array() syntax, ready to paste into Laravel config files, seeders and tests. cURL to Axios (Node.js) Converter Convert curl commands to Node.js axios requests with params, headers, JSON data, auth, form-data uploads and timeouts. cURL to C# Converter Convert curl commands to C# HttpClient code for .NET with headers, content types, basic auth, multipart and TLS options. cURL to Go Converter Convert curl commands into Go code using net/http — method, headers, JSON or form body, basic auth, timeouts and multipart uploads. cURL to PHP Guzzle Converter Convert curl commands to PHP Guzzle requests using json, form_params, multipart, query and auth options. cURL to Python Requests Convert curl commands into Python requests code: headers, params, JSON or form data, files, cookies, auth and timeouts. cURL to Rust Converter Convert curl commands to Rust using the reqwest crate, with JSON bodies via serde_json, headers, auth and timeouts. Date & Time Calculator Calculate the difference between two dates or add/subtract days, weeks, months and years from a date. Docker Run to Docker Compose Convert one or more docker run commands into a docker-compose.yml: ports, volumes, env, restart, networks, healthchecks and more. File Type Checker Find out what a file really is from its content, not its name. Detects 150+ formats and warns when the extension does not match. HTTP Status Codes Searchable list of the HTTP status codes in use, from 100 to 511, with a plain explanation of when each response is used.