curl to PHP Converter – Free, Online, Private
Convert a curl command into a PHP cURL script. The URL, method, headers, and body become curl_setopt calls you can read.
How to convert curl to PHP
- Paste a curl command. The tokenizer handles quotes and backslash line continuations.
- Copy the PHP. It initializes a handle, sets the options, executes, and prints the status and body.
- Turn TLS verification back on if you copied a command that used -k, and do not leave a real token in the file.
The script
The output starts with <?php and a curl_init. CURLOPT_URL is the parsed URL, with http:// added if the command used a bare host, because that is what curl does. CURLOPT_RETURNTRANSFER is true so curl_exec returns the body instead of writing it. CURLOPT_CUSTOMREQUEST is the method, including POST inferred from -d when -X was absent. Headers become a CURLOPT_HTTPHEADER list of 'Name: value' strings. A JSON or raw body is CURLOPT_POSTFIELDS with a single-quoted PHP string. -F fields become an array of strings, or CURLFile when the value was @file. -u sets CURLOPT_USERPWD. -L sets CURLOPT_FOLLOWLOCATION. -k sets CURLOPT_SSL_VERIFYPEER to false, visibly. --compressed sets CURLOPT_ENCODING to an empty string, which is curl's way of asking for every encoding it supports.
Quotes and continuations
Copied curl almost always breaks lines with a trailing backslash. Those joins are removed before tokens are read, so the backslash does not become part of the URL or the header. Single quotes copy their contents literally, which is why a JSON body in single quotes keeps its double quotes. Double quotes honor escapes. $'...' sequences such as \n become real newlines. The PHP string is then escaped for single quotes, so a body that contains an apostrophe does not break the script. Shell variables are not expanded. If the command says Bearer $TOKEN, the PHP string contains dollar-TOKEN. Substitute the value yourself. The parser rejects an unterminated quote instead of emitting a script that would send half a header.
Worked example
The sample POST sets the users URL, the method POST, the bearer header, the JSON content type, and the body with Ada's name and role. The status is read with CURLINFO_HTTP_CODE and printed before the body. curl_close runs after the exec. There is no curl_error check. Add one if you keep the script. The token in the sample is the placeholder TOKEN. A command copied from a browser can contain a session cookie. That cookie is a header in the PHP, and it stays in your browser tab while you edit it. The page does not have a server to store it. The Python, Go, and Axios pages parse the same command, so you can compare the header list if a conversion looks short.
Flags that stay out
Proxies, retries, max-time, cookie jars, and -o output files are skipped. A skipped flag with a value is consumed so it does not become the URL, but it does not generate a curl_setopt. File bodies referenced with -d @file are kept as the literal characters, with a warning, because the page cannot read your disk and should not pretend the file contents were in the command. Multipart file fields use CURLFile with the path you wrote. The script does not set CURLOPT_TIMEOUT. A missing timeout can hang a PHP request until the server's own limit. Add one. Generation is client-side, same as the other converters. Paste a command that contains an internal hostname if you need to. It is not uploaded.
Frequently asked questions
Why CUSTOMREQUEST instead of POST?
CUSTOMREQUEST keeps the method you wrote, including PUT and PATCH. POSTFIELDS still carries the body.
Does -k survive?
Yes. CURLOPT_SSL_VERIFYPEER is set to false only when the command passed -k or --insecure.
Are backslash continuations required?
No. They are accepted because copied commands use them. One long line works too.
Is the cookie uploaded?
No. The script is built in your browser. Close the tab and the command is gone.