Convert curl to PowerShell Invoke-WebRequest (and PHP)
Turn a curl command into PowerShell Invoke-WebRequest or PHP with a flag-by-flag mapping table, a worked example, converter options and fixes for common errors.
GuideUpdated 5 min read
On this page
You have a working curl command from API docs or your browser's developer tools, and you need the same request in PowerShell (Invoke-WebRequest, or IWR) or in PHP. Most people searching for this want a mapping they can trust, not a lecture on HTTP.
Why curl commands don't paste into PowerShell
curl and Invoke-WebRequest describe the same HTTP request in different vocabularies. Three things trip people up:
- The
curlalias. In Windows PowerShell 5.1,curlis an alias forInvoke-WebRequest. Your curl flags are read as IWR parameters and fail with an "unknown parameter" style error. PowerShell 7 does not define this alias. - Quoting rules. Shell quoting for curl (single quotes, backslash line breaks) is not PowerShell quoting (backtick line breaks,
$expansion inside double quotes). - Implicit behavior. curl sends some headers on its own. For example,
-ddata goes out as form-encoded unless you set a content type. A translation has to reproduce that on purpose.
Map the flags by hand
| curl | PowerShell Invoke-WebRequest | PHP cURL option |
|---|---|---|
-X POST | -Method Post | CURLOPT_CUSTOMREQUEST => 'POST' |
-H "Name: value" | -Headers @{ Name = "value" } | CURLOPT_HTTPHEADER => ['Name: value'] |
-H "Content-Type: …" | -ContentType "…" | include it in CURLOPT_HTTPHEADER |
-d '…' / --data-raw | -Body '…' | CURLOPT_POSTFIELDS => '…' |
-u user:pass | Authorization: Basic … header (see below) | CURLOPT_USERPWD => 'user:pass' |
-A "agent" | -UserAgent "agent" | CURLOPT_USERAGENT => 'agent' |
-L | control with -MaximumRedirection | CURLOPT_FOLLOWLOCATION => true |
-k | -SkipCertificateCheck (PowerShell 7 and later only) | CURLOPT_SSL_VERIFYPEER => false |
-I | -Method Head | CURLOPT_NOBODY => true |
-o file | -OutFile file | save the result with file_put_contents() |
-F name=value | -Form @{ name = "value" } (PowerShell 7 and later) | CURLOPT_POSTFIELDS with an array (use CURLFile for files) |
For redirect behavior, read the Invoke-WebRequest page on Microsoft Learn rather than assuming it matches curl.
Worked example
Here is a typical command:
curl -X POST "https://api.example.com/items" \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"widget"}'PowerShell version
$headers = @{ Authorization = "Bearer TOKEN" }
$body = '{"name":"widget"}'
Invoke-WebRequest -Uri "https://api.example.com/items" `
-Method Post `
-Headers $headers `
-ContentType "application/json" `
-Body $bodyNotes on this version:
- Single quotes around the JSON keep the inner double quotes intact.
- The backtick at the end of a line continues the command. Put nothing after it, not even a space.
Invoke-WebRequestreturns an object. The response text is in.Content.- If the API returns JSON and you want it parsed into objects, use
Invoke-RestMethodwith the same parameters.
PHP version
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.example.com/items',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer TOKEN',
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => '{"name":"widget"}',
]);
$response = curl_exec($ch);
if ($response === false) {
echo curl_error($ch);
}
echo $response;CURLOPT_RETURNTRANSFER makes curl_exec() return the body instead of printing it. Without it, the response prints directly and curl_exec() returns true.
Basic auth in PowerShell
curl's -u user:pass sends a Basic Authorization header. The most direct translation builds that header yourself:
$pair = "user:pass"
$b64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($pair))
Invoke-WebRequest -Uri "https://api.example.com/me" -Headers @{ Authorization = "Basic $b64" }Use a converter instead
| Option | Type | Good for | Trade-off |
|---|---|---|---|
| curlconverter | Web page and command-line tool, free, open source | Many output languages, including PowerShell and PHP (as of November 2026) | Converts curl commands only; not a Mac app |
| Postman | Desktop API client | Users report importing the raw curl text, then opening the code view to pick a language | Far more than a one-off conversion needs; labels vary by version |
| By hand (table above) | None | Short commands, learning the mapping | Easy to miss implicit headers |
Whichever you use, a pasted curl command usually contains a token or password. Replace secrets with placeholders before pasting into any web page, then put the real values back afterward.
If the converted code doesn't work
PowerShell says a parameter is unknown
You probably ran the curl command itself in Windows PowerShell 5.1, where curl means Invoke-WebRequest. Call curl.exe to run real curl, or translate the command as above.
The server returns 400 or 415
The content type is a common cause. Without -H "Content-Type: …", curl sends -d data as form-encoded. Set -ContentType (PowerShell) or the Content-Type header (PHP) to match what the API expects.
The JSON body arrives mangled in PowerShell
Double-quoted strings expand $variables and treat backticks specially. Use single quotes, or a here-string (@' on its own line, then the JSON, then '@ on its own line).
-SkipCertificateCheck isn't recognized
It exists only in PowerShell 7 and later, not in Windows PowerShell 5.1. If you can, fix the certificate problem rather than turning checks off.
IWR fails with an Internet Explorer engine error
In Windows PowerShell 5.1, add -UseBasicParsing.
PHP reports Call to undefined function curl_init()
The cURL extension isn't enabled in your PHP install. Enable it, then restart PHP.
PHP returns false and no output
Print curl_error($ch), as in the example, to see why.
Checklist before you run the converted request
- The method matches (
-Xbecame-MethodorCURLOPT_CUSTOMREQUEST). - Every header from the curl command is present, including
Content-Type. - The body is quoted so the JSON survives (single quotes in PowerShell).
- Secrets are placeholders in anything you paste online or commit.
- You are on the PowerShell version your flags need (7 or later for
-SkipCertificateCheckand-Form).
If nothing works
If the original curl command works and your translation still fails, the difference is in a header, the body encoding or TLS. Compare curl -v output with what your PowerShell or PHP code sends to a test endpoint you control. Then contact the API vendor's support, or ask your team, with a message like this:
My curl command succeeds, but the equivalent PowerShell Invoke-WebRequest call
returns HTTP <status code>.
PowerShell version: <output of $PSVersionTable.PSVersion>
Endpoint: <URL, without secrets>
Headers sent (curl -v output, secrets removed): <paste>
Error text: <exact message>
What I tried: matching Content-Type, single-quoted body, -UseBasicParsingCommon questions
Should I use Invoke-WebRequest or Invoke-RestMethod?
Use Invoke-RestMethod when the API returns JSON or XML and you want parsed objects. Use Invoke-WebRequest when you need the status code, headers or raw content.
Does PHP need the cURL extension?
Yes, for curl_init() and related functions. If it is missing, enable the extension in your PHP install and restart PHP.
How do I run real curl in Windows PowerShell 5.1?
Type curl.exe instead of curl. That bypasses the alias and runs the curl executable, if one is installed on your system.