Skip to content

curl to PHP Converter

Paste a curl command and get PHP code using PHP's built-in cURL extension, with every option set in one curl_setopt_array call.
<?php

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => 'https://jsonplaceholder.typicode.com/posts',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Authorization: Bearer YOUR_TOKEN',
    ],
    CURLOPT_POSTFIELDS => '{"title": "Hello", "body": "From curl", "userId": 1}',
]);

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

echo $status, PHP_EOL, $response;

Converted in your browser. Commands often contain API keys, so nothing is sent anywhere or saved.

How to convert curl to PHP

  1. Paste your curl command (from API docs, a terminal or your browser's dev tools).
  2. The PHP code appears instantly, using cURL. Switch tabs to see other languages.
  3. Copy the code and run it; no extra packages are needed.

How curl options map to cURL

curlPHP
-X PUTCURLOPT_CUSTOMREQUEST => 'PUT'
-H 'Name: value'CURLOPT_HTTPHEADER => [...]
-d '...'CURLOPT_POSTFIELDS => '...'
-F 'file=@photo.jpg'CURLOPT_POSTFIELDS => ['file' => new CURLFile(...)]
-u user:passCURLOPT_USERPWD => 'user:pass'
-LCURLOPT_FOLLOWLOCATION => true
-m 10CURLOPT_TIMEOUT => 10
-ICURLOPT_NOBODY => true

Tips for the generated PHP code

  • CURLOPT_RETURNTRANSFER => true makes curl_exec return the body as a string instead of printing it.
  • curl_exec returns false on network errors; check it and read curl_error() for the reason.
  • Decode JSON responses with json_decode($response, true) to get an associative array.

Convert curl to other languages

The same converter writes Python, JavaScript, Node.js (axios), Go and Ruby. See the cURL converter for every supported option.

Need an API to try it on? Every API in our list of APIs with no key works with a plain curl command.

Frequently asked questions

What's the difference between a string and an array in CURLOPT_POSTFIELDS?

A string is sent exactly as given (like curl -d), so you set the Content-Type yourself. An array is sent as multipart/form-data (like curl -F), which is what file uploads with CURLFile need.

Why do I get "SSL certificate problem" in PHP but not in curl?

PHP may be using an outdated CA bundle. Point curl.cainfo in php.ini at a current cacert.pem rather than turning off CURLOPT_SSL_VERIFYPEER, which makes the connection insecure.

Should I use Guzzle instead of cURL in PHP?

Guzzle wraps cURL with a nicer API, PSR-7 messages and middleware, and is the standard in Laravel and Symfony apps. The built-in cURL functions need no dependencies, which suits scripts and small projects.

How do I copy a request as cURL from Chrome?

Open DevTools (F12), go to the Network tab and reload. Right-click the request, choose Copy, then Copy as cURL (bash). Paste it here. Chrome's Windows (cmd) format works too.

Is it safe to paste commands that contain API keys?

Yes. The conversion runs entirely in your browser; nothing you paste is sent to a server, logged or saved. Still, rotate any key you've shared publicly.

More free developer tools