cURL to PHP Converter
Convert cURL commands to PHP curl_exec() code — paste and get runnable PHP instantly
You have a cURL command and need to reproduce the same HTTP request in PHP. Rather than manually looking up every CURLOPT_* constant and calling curl_setopt() for each one, paste your cURL command here and get ready-to-run PHP code using PHP’s built-in cURL extension.
PHP’s Built-in cURL Extension
PHP has shipped with a native cURL extension since version 4.0.2. It wraps the same libcurl library that the curl command-line tool uses, which means translated code behaves identically to the original request in most situations. The extension is enabled by default in most PHP distributions and hosting environments.
Verify cURL is available:
php -m | grep curl
If the extension is missing, enable it by uncommenting extension=curl in your php.ini and restarting the web server.
How the Converter Maps cURL Flags to PHP
| cURL flag | PHP CURLOPT equivalent |
|---|---|
-X PUT | CURLOPT_CUSTOMREQUEST, "PUT" |
-H "Accept: application/json" | CURLOPT_HTTPHEADER, ["Accept: application/json"] |
-d '{"x":1}' | CURLOPT_POSTFIELDS, '{"x":1}' |
-u user:pass | CURLOPT_USERPWD, "user:pass" |
-b "session=abc" | CURLOPT_COOKIE, "session=abc" |
-L / --location | CURLOPT_FOLLOWLOCATION, true |
-k / --insecure | CURLOPT_SSL_VERIFYPEER, false |
The generated code always sets CURLOPT_RETURNTRANSFER to true so the response is returned as a string rather than printed directly. It also uses curl_getinfo($ch, CURLINFO_HTTP_CODE) to retrieve the HTTP status code separately from the body.
Complete PHP cURL Workflow
Every generated script follows the standard four-step pattern:
curl_init()— initialize a cURL handlecurl_setopt()/curl_setopt_array()— configure the requestcurl_exec()— execute the requestcurl_close()— free the handle
This is equivalent to creating, configuring, sending, and closing an HTTP connection.
Handling JSON Responses in PHP
The generated code echoes the raw response string. To parse a JSON API response:
$data = json_decode($response, true); // true = associative array
echo $data['key'];
Always check json_last_error() after decoding if the response may not be valid JSON:
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('JSON decode error: ' . json_last_error_msg());
}
Error Handling
The generated code does not include error handling for brevity. In production, always check for cURL errors:
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
throw new RuntimeException("cURL error: $error");
}
curl_exec() returns false on a network-level failure (DNS failure, connection refused). It returns the response body even for 4xx and 5xx HTTP status codes — use curl_getinfo($ch, CURLINFO_HTTP_CODE) to check the status.
Timeouts and Connection Settings
For production scripts, always set timeouts to prevent hanging requests:
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // seconds to connect
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // total execution time
Frequently Asked Questions
Can I reuse the cURL handle for multiple requests?
Yes. Call curl_reset($ch) to reset all options on an existing handle, then configure and execute again. This reuses the underlying TCP connection if keep-alive is enabled, which is more efficient than curl_init() for each request.
How do I send a file upload with PHP cURL?
Use CURLOPT_POSTFIELDS with a CURLFile object: new CURLFile('/path/to/file.txt', 'text/plain', 'filename.txt'). The converter handles --data bodies; multipart uploads require this additional step.
Why does PHP’s cURL return a different response than the command-line curl?
Check the User-Agent header. PHP’s cURL sends PHP/8.x by default, while the command-line curl sends curl/8.x. Some servers serve different content or block certain agents. Set CURLOPT_USERAGENT to match the desired User-Agent string.