DevToolsmith

cURL to PHP Converter

Convert curl commands to PHP code with the built-in cURL extension. The result uses curl_setopt_array with the CURLOPT equivalent of every flag: headers, POST fields, JSON, file uploads with CURLFile, basic and digest auth, SSL options and timeouts.

Runs 100% in your browser — nothing is uploaded

curl command
Tip: drop a file here236 chars · 236 bytes · 4 lines
PHP · cURL extension
615 chars · 615 bytes · 22 lines
Parsed request

POST https://api.example.com/v1/orders?notify=true

  • Authorization: Bearer YOUR_API_TOKEN
  • Content-Type: application/json
  • Body: JSON

Example

curl
curl -X POST https://api.example.com/v1/charges \
  -u sk_test_YOUR_KEY: \
  -d amount=2000 \
  -d currency=usd \
  -d 'description=Order #1001'
PHP (cURL extension)
<?php

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.example.com/v1/charges',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/x-www-form-urlencoded',
    ],
    CURLOPT_POSTFIELDS => 'amount=2000&currency=usd&description=Order #1001',
    CURLOPT_USERPWD => 'sk_test_YOUR_KEY:',
]);

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

echo $status, PHP_EOL, $response;

curl flags and their CURLOPT options

curlPHP option
-X PUTCURLOPT_CUSTOMREQUEST => 'PUT'
-H 'A: b'CURLOPT_HTTPHEADER => ['A: b']
-d 'a=1'CURLOPT_POSTFIELDS => 'a=1' (implies POST)
-F 'f=@x.png'CURLOPT_POSTFIELDS => ['f' => new CURLFile('x.png')]
-u user:passCURLOPT_USERPWD => 'user:pass'
--digestCURLOPT_HTTPAUTH => CURLAUTH_DIGEST
-LCURLOPT_FOLLOWLOCATION => true
-kCURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 0
-m 10CURLOPT_TIMEOUT => 10
--connect-timeout 3CURLOPT_CONNECTTIMEOUT => 3
--compressedCURLOPT_ENCODING => '' (all supported encodings)
-x proxy:3128CURLOPT_PROXY => 'proxy:3128'
-ICURLOPT_NOBODY => true
-vCURLOPT_VERBOSE => true

Decoding a JSON response

PHP
$data = json_decode($response, true, flags: JSON_THROW_ON_ERROR);

if ($status >= 400) {
    throw new RuntimeException("HTTP $status: " . ($data['error']['message'] ?? $response));
}

curl_exec returns false only for transport errors (DNS, connection, TLS). A 404 or 500 is still a successful call, so check the status code as above.

Frequently asked questions

Do I need a library to make HTTP requests in PHP?

No. The cURL extension (ext-curl) ships with almost every PHP installation and is what this converter uses. Its options map one to one onto curl’s command-line flags, because both are built on libcurl. In Laravel or Symfony you can use their HTTP clients or Guzzle instead; the requests are the same.

How do I send JSON with PHP cURL?

Set CURLOPT_POSTFIELDS to the JSON string, for example json_encode($data), and add a Content-Type: application/json header with CURLOPT_HTTPHEADER. If you pass an array to CURLOPT_POSTFIELDS instead, PHP sends multipart/form-data, which is a common mistake.

Array or string for CURLOPT_POSTFIELDS?

A string is sent as is, which is right for URL-encoded forms (http_build_query($fields)) and JSON. An array makes PHP send multipart/form-data, which is right for file uploads with CURLFile, like curl -F. The converter picks the one that matches your curl command.

Should I call curl_close()?

Not anymore. Since PHP 8.0 a cURL handle is an object that is freed automatically, and curl_close() does nothing; it is deprecated as of PHP 8.5. The generated code leaves it out.

How do I see what PHP cURL is sending?

Set CURLOPT_VERBOSE => true to print the request and response headers to stderr, like curl -v. To capture request headers in code, set CURLINFO_HEADER_OUT => true and read curl_getinfo($ch, CURLINFO_HEADER_OUT) after curl_exec.