How to convert curl to PHP
- Paste your curl command (from API docs, a terminal or your browser's dev tools).
- The PHP code appears instantly, using cURL. Switch tabs to see other languages.
- Copy the code and run it; no extra packages are needed.
How curl options map to cURL
| curl | PHP |
|---|---|
-X PUT | CURLOPT_CUSTOMREQUEST => 'PUT' |
-H 'Name: value' | CURLOPT_HTTPHEADER => [...] |
-d '...' | CURLOPT_POSTFIELDS => '...' |
-F 'file=@photo.jpg' | CURLOPT_POSTFIELDS => ['file' => new CURLFile(...)] |
-u user:pass | CURLOPT_USERPWD => 'user:pass' |
-L | CURLOPT_FOLLOWLOCATION => true |
-m 10 | CURLOPT_TIMEOUT => 10 |
-I | CURLOPT_NOBODY => true |
Tips for the generated PHP code
- CURLOPT_RETURNTRANSFER => true makes curl_exec return the body as a string instead of printing it.
- curl_exec returns false on network errors; check it and read curl_error() for the reason.
- Decode JSON responses with json_decode($response, true) to get an associative array.
Convert curl to other languages
The same converter writes Python, JavaScript, Node.js (axios), Go and Ruby. See the cURL converter for every supported option.
Need an API to try it on? Every API in our list of APIs with no key works with a plain curl command.