Convert cURL to PHP

Paste a curl command and get PHP code that uses PHP’s own cURL functions — curl_init, curl_setopt_array and curl_exec. No Composer packages needed.

AI DevRuns in your browser — nothing uploaded

Output language

Converted in your browser — tokens and passwords in the command never leave your device. Tip: in Chrome DevTools, right-click a request in the Network tab → Copy → Copy as cURL.

How curl options map to PHP

PHP’s cURL extension is libcurl, the same library the curl command uses, so almost every option has a direct match.

  1. The URL goes in CURLOPT_URL. CURLOPT_RETURNTRANSFER => true makes curl_exec return the response instead of printing it.
  2. -H headers go in CURLOPT_HTTPHEADER as an array of 'Name: value' strings.
  3. -d becomes CURLOPT_POSTFIELDS. Setting it also makes the request a POST. Form fields are built with http_build_query, which gives the same encoded string curl sends.
  4. JSON bodies sit in a nowdoc (<<<'JSON'), so quotes and backslashes need no escaping.
  5. -F becomes an array in CURLOPT_POSTFIELDS, with new CURLFile(...) for each file. Passing an array is what makes PHP send multipart/form-data.
  6. -X PUT and other methods use CURLOPT_CUSTOMREQUEST. -u becomes CURLOPT_USERPWD, -b CURLOPT_COOKIE, --max-time CURLOPT_TIMEOUT and -L CURLOPT_FOLLOWLOCATION.
  7. -k turns off CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST. Only do that for local testing.

curl_exec returns false only when the request couldn’t be made — DNS, timeout or TLS errors. A 404 or 500 still returns the body, so check the status from curl_getinfo. You don’t need curl_close(): the handle has closed itself since PHP 8.0, and PHP 8.5 deprecates the function.

Example: curl to PHP

This command, pasted into the converter above, gives the code below.

cURL command
curl https://api.example.com/v1/customers \
  -u 'YOUR_SECRET_KEY:' \
  -d email=asha@example.com \
  -d name='Asha Rao' \
  -d 'metadata[plan]=pro'
PHP
<?php

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.example.com/v1/customers',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/x-www-form-urlencoded',
    ],
    CURLOPT_POSTFIELDS => http_build_query([
        'email' => 'asha@example.com',
        'name' => 'Asha Rao',
        'metadata[plan]' => 'pro',
    ]),
    CURLOPT_USERPWD => 'YOUR_SECRET_KEY:',
    CURLOPT_TIMEOUT => 30,
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new RuntimeException(curl_error($ch));
}
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);

echo $status, PHP_EOL;
echo $response, PHP_EOL;

How it works

  1. 1

    Reads the command like a shell

    Single and double quotes, $'…' strings, backslash line breaks and Windows ^ escapes are all understood — so Chrome’s “Copy as cURL (bash)” and “(cmd)” both work.

  2. 2

    Understands curl’s options

    -X, -H, -d, --data-raw, --data-urlencode, --json, -F, -u, -b, -G, -k, -L, -x, --max-time and more — with curl’s own defaults, like POST when there’s a body.

  3. 3

    Writes idiomatic code

    json= in Python, URLSearchParams and FormData in JavaScript, a multipart writer in Go, CURLFile in PHP and a text block in Java — the way each language would normally do it, using only what ships with it where possible.

  4. 4

    Tested against real curl

    Code in all seven languages was run against a test server and checked to send the same method, URL, headers and body as curl itself.

Questions, answered

No. The cURL extension is enabled on almost every PHP host and needs no Composer package. If your project already uses Guzzle or Laravel’s Http client, the same parts map across: headers to headers, a JSON body to json, form fields to form_params.

Popular uses

Read the guide