cURL to PHP

Convert a cURL command into a PHP curl_setopt call.

cURL Command
PHP (curl) Output
PHP (curl) code appears here

Related Tools

Documentation

What is curl-to-PHP?

This tool turns a curl command into a PHP script built on the curl extension — curl_init, curl_setopt, curl_exec — which ships with virtually every PHP install, so the output runs without Composer or Guzzle.

How it works

Parsing walks the curl flags (-X, -H, -d/--data, -F/--form, -u/--user, -k/--insecure) into a request description, and the PHP emitter turns each field into one curl_setopt call: CURLOPT_URL, CURLOPT_CUSTOMREQUEST for the method, CURLOPT_POSTFIELDS for the body, CURLOPT_HTTPHEADER for headers, and CURLOPT_USERPWD for Basic Auth. The request body from -d is dropped into CURLOPT_POSTFIELDS as a raw PHP string literal, exactly as curl would send it over the wire — it's never parsed and rebuilt as a PHP array, so nested JSON, unusual key ordering, or numeric formatting all survive untouched.

Features

  • Uses PHP's built-in curl extension — no Guzzle or Composer dependency
  • Body passed through as a raw string via CURLOPT_POSTFIELDS
  • -F file fields (values starting with @) become CURLFile objects
  • -u Basic Auth mapped to CURLOPT_USERPWD
  • -k/--insecure mapped to CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST both set false
  • Minimal curl_error() check on failure

Example

Input: curl -X POST https://api.example.com/v1/users -H "Content-Type: application/json" -H "X-Api-Key: abc123" -d '{"name":"Ada Lovelace","role":"admin"}'

Output:

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.example.com/v1/users');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, '{"name":"Ada Lovelace","role":"admin"}');

curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'X-Api-Key: abc123',
]);

$response = curl_exec($ch);
if ($response === false) {
    echo curl_error($ch);
}
curl_close($ch);

echo $response;

Common errors

The generated script only checks the transport-level failure (curl_exec returning false) — it does not check the HTTP status code, so a 404 or 500 response still prints as "success" from curl's point of view. Add curl_getinfo($ch, CURLINFO_HTTP_CODE) yourself if you need to branch on status. A body that's an array-like key=value&key2=value2 string (from multiple -d flags) is still passed as one raw string, not an associative array, which changes the Content-Type curl sends by default unless you set it explicitly with -H.

Best practices

Always check the HTTP status alongside curl_error() before trusting the response body. Never ship CURLOPT_SSL_VERIFYPEER set to false to production — it's only appropriate for local development against a self-signed certificate you control.

Frequently Asked Questions

Does this need the Guzzle library?

No — it uses PHP's built-in curl extension (curl_init, curl_setopt, curl_exec) directly, which ships with virtually every PHP install. Guzzle is a nicer API for larger projects, but isn't required for a single request.

How are file uploads (-F with @file) handled?

Wrapped in a CURLFile object, PHP's standard way of attaching a file to a multipart POST via CURLOPT_POSTFIELDS.

Is error handling included?

A minimal check is included — curl_exec returning false triggers curl_error($ch) — but production code should also check the HTTP status code via curl_getinfo($ch, CURLINFO_HTTP_CODE), which isn't added automatically here to keep the output focused on the request itself.

What does -k / --insecure map to?

CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST both set to false — PHP curl's equivalent of disabling certificate verification. Only appropriate against endpoints you trust.