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
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.
- The URL goes in
CURLOPT_URL.CURLOPT_RETURNTRANSFER => truemakescurl_execreturn the response instead of printing it. -Hheaders go inCURLOPT_HTTPHEADERas an array of'Name: value'strings.-dbecomesCURLOPT_POSTFIELDS. Setting it also makes the request a POST. Form fields are built withhttp_build_query, which gives the same encoded string curl sends.- JSON bodies sit in a nowdoc (
<<<'JSON'), so quotes and backslashes need no escaping. -Fbecomes an array inCURLOPT_POSTFIELDS, withnew CURLFile(...)for each file. Passing an array is what makes PHP send multipart/form-data.-X PUTand other methods useCURLOPT_CUSTOMREQUEST.-ubecomesCURLOPT_USERPWD,-bCURLOPT_COOKIE,--max-timeCURLOPT_TIMEOUTand-LCURLOPT_FOLLOWLOCATION.-kturns offCURLOPT_SSL_VERIFYPEERandCURLOPT_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 https://api.example.com/v1/customers \
-u 'YOUR_SECRET_KEY:' \
-d email=asha@example.com \
-d name='Asha Rao' \
-d 'metadata[plan]=pro'<?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
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
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
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
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.
Put the JSON string in CURLOPT_POSTFIELDS and add 'Content-Type: application/json' to CURLOPT_HTTPHEADER. If you’re building the data in PHP, use json_encode($data) to make the string.
Older code used '@/path/to/file' in POSTFIELDS. PHP 7 removed that, so the string is now sent as plain text. Use new CURLFile('/path/to/file') instead — the converter does this for every -F file.
Set CURLOPT_HEADER => true; the headers then come before the body in the response. Split them off with curl_getinfo($ch, CURLINFO_HEADER_SIZE), or collect them with CURLOPT_HEADERFUNCTION.
Almost always — cURL is enabled by default on most hosts. To check, run var_dump(extension_loaded('curl')); it should print bool(true).
Popular uses
Read the guide
Related tools
- Prompt CompressorCut tokens from prompts and code before you send them to an LLM.AI Dev
- Regex ExplainerPaste a regex, get plain English plus test cases.AI Dev
- AI SQL Query GeneratorEnglish to SQL for Postgres, MySQL and MSSQL.AI Dev
- JSON to Pydantic / TSInstant Pydantic models, TypeScript types or dataclasses.AI Dev