Turn curl commands into working PHP
API documentation almost always shows requests as curl commands, and browser developer tools can copy any request "as cURL". Translating those flags into PHP's curl_setopt calls by hand means looking up constant names every time. This curl to PHP converter parses the command and writes the equivalent PHP using the built-in curl extension, with no dependencies.
Example
curl -X POST 'https://api.example.com/v1/orders?notify=true' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer YOUR_API_TOKEN' \
-d '{"sku":"LAMP-01","qty":2}' \
--max-time 30<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.example.com/v1/orders?notify=true');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"sku":"LAMP-01","qty":2}');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer YOUR_API_TOKEN',
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
} else {
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $response;
}
curl_close($ch);
How options map to PHP
-X PUT→CURLOPT_CUSTOMREQUEST; a POST with a body usesCURLOPT_POST;-IusesCURLOPT_NOBODY.- Headers →
CURLOPT_HTTPHEADER; cookies →CURLOPT_COOKIE. -d→CURLOPT_POSTFIELDSas a string;-F→ an array, with@fileuploads converted tonew CURLFile(…).-u user:pass→CURLOPT_USERPWD;--compressed→CURLOPT_ENCODING.-L→CURLOPT_FOLLOWLOCATION;-mand--connect-timeout→ timeout options;-x→CURLOPT_PROXY.-k→ disables peer and host verification. Use it only for local testing; never ship it to production, because it makes HTTPS connections vulnerable to interception.
Supported curl options
| curl option | Meaning |
|---|---|
-X, --request | HTTP method (GET, POST, PUT, PATCH, DELETE…) |
-H, --header | Request headers |
-d, --data, --data-raw, --data-binary, --data-urlencode | Request body; implies POST |
--json | JSON body with Content-Type and Accept headers |
-F, --form | Multipart form fields and file uploads |
-u, --user and --oauth2-bearer | Basic and bearer authentication |
-b, --cookie, -A, -e | Cookies, user agent and referer |
-G, --get | Send -d data as query string |
-L, -k, -m, -x, -I, -T | Follow redirects, skip TLS checks, timeout, proxy, HEAD request, upload file |
Line continuations (\), single and double quotes are parsed like a Unix shell. Options that only affect curl's own output, such as -s, -v or -o, are ignored silently; any other unsupported option is listed as a comment at the top of the generated code, so nothing is dropped without notice.
Where to get curl commands
Besides API documentation, the quickest source is your browser. Open the developer tools, switch to the Network tab, right-click a request and choose "Copy as cURL". The command includes every header the browser sent, including cookies and session tokens, so remove the ones you do not need before converting and never share them.
Making the code production-ready
- Move tokens and passwords into environment variables instead of hard-coding them.
- Check
$statusand handle 4xx/5xx responses, not just transport errors. - Decode JSON responses with
json_decode($response, true)and handle failures. - In a framework, consider its HTTP client: Laravel's
Httpfacade and Symfony HttpClient wrap the same concepts with less code.
Other languages: cURL to Python and cURL to JavaScript. To try a request directly from the browser, use the API tester. The command is parsed locally, so tokens in it are never sent to this site.