Convert Curl to PHP

Convert curl commands to PHP curl code instantly. Free online curl to PHP converter that handles headers, authentication, and POST data.

Why converting curl to PHP is straightforward

PHP's curl extension is a direct wrapper around libcurl, the same library that powers the curl command-line tool. This means the mapping between curl flags and PHP code is almost one-to-one. A -H header flag becomes CURLOPT_HTTPHEADER, a -d body becomes CURLOPT_POSTFIELDS, and -u user:pass becomes CURLOPT_USERPWD.

Despite this close relationship, writing the PHP boilerplate by hand is repetitive. Every request needs curl_init(), a series of curl_setopt() calls, curl_exec(), and curl_close(). This converter generates all of that from a single paste.

The generated code runs with PHP's built-in curl extension. No Composer packages or external libraries required.

How curl flags map to PHP curl options

Curl flag PHP curl option
-X POST CURLOPT_CUSTOMREQUEST
-H 'Key: Value' CURLOPT_HTTPHEADER array
-d '{"key":"val"}' CURLOPT_POSTFIELDS
-u user:pass CURLOPT_USERPWD
-L CURLOPT_FOLLOWLOCATION
-k CURLOPT_SSL_VERIFYPEER = false
-b 'cookie=val' Added to CURLOPT_HTTPHEADER as Cookie

The converter always sets CURLOPT_RETURNTRANSFER to true so that curl_exec() returns the response body as a string instead of outputting it directly. This is the standard pattern for API calls in PHP.

PHP curl vs Guzzle

The generated code uses PHP's built-in curl_* functions. This is the right choice when you want zero dependencies, need fine-grained control over the request, or are working in a minimal PHP environment without Composer.

If your project already uses Guzzle (the most popular PHP HTTP client), the generated code still serves as a reference for the headers, body, and auth values. You can translate the curl_setopt() calls to Guzzle's request options using the same values.

Error handling in the generated code

The converter generates minimal code focused on correctness. For production use, add error checking after curl_exec():

if ($response === false) {
    echo 'Curl error: ' . curl_error($ch);
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode >= 400) {
    echo 'HTTP error: ' . $httpCode;
}

The generated code already captures the HTTP status code via curl_getinfo(). Adding the error check above gives you visibility into both curl-level failures (DNS errors, timeouts) and HTTP-level errors (4xx, 5xx responses).

Frequently Asked Questions

How do I convert curl to PHP?

Paste your curl command into the input field above, select "PHP," and click Convert. The output uses PHP's built-in curl_* functions and is ready to use in any PHP project with the curl extension enabled.

Does the generated code require any PHP packages?

No. The code uses PHP's built-in curl extension, which is enabled by default on most PHP installations. No Composer packages are needed.

Can I use this with Laravel or Symfony?

Yes. The generated code runs in any PHP environment. If you prefer using your framework's HTTP client (Laravel's Http facade or Symfony's HttpClient), the headers and body values from the generated code translate directly.

Is my curl command sent to a server?

No. All conversion happens in your browser. Nothing leaves your machine.

How does the converter handle SSL verification?

If the curl command includes -k or --insecure, the converter sets CURLOPT_SSL_VERIFYPEER to false and CURLOPT_SSL_VERIFYHOST to 0. Otherwise, SSL verification is left at PHP's default (enabled).

Related Free Tools

Give your AI agent a faster, leaner browser

Structured page data instead of raw HTML. Your agent processes less, decides faster, and costs less to run.

Stability detection built in
Fraction of the payload size
Diffs after every action
No credit card required. 1 hour of free runtime included.