cURL to PHP Code Converter
Convert cURL commands to PHP code instantly. Get working PHP cURL or Guzzle HTTP snippets with all headers, authentication, and body parameters preserved — ready to paste into your PHP project.
Why Convert cURL to PHP
PHP has native cURL bindings but they require verbose curl_setopt() chains to configure. Converting an API request from the command line or browser DevTools to PHP means mapping each cURL flag to the right CURLOPT_* constant. This converter handles that mapping automatically: -H headers become the CURLOPT_HTTPHEADER array, -d body becomes CURLOPT_POSTFIELDS, and Bearer auth maps to the correct header format. The output compiles and runs without modification.
- PHP cURL output: Native
curl_init()/curl_setopt()code — no dependencies - All HTTP methods: GET, POST, PUT, PATCH, DELETE, and custom methods
- Headers preserved: Converted to proper
CURLOPT_HTTPHEADERarray format - Auth support: Bearer tokens, Basic auth, and API key headers translated correctly
- JSON body: Request body preserved as string for
CURLOPT_POSTFIELDS
Choose the Right Variant
- This page: PHP native cURL extension — works everywhere PHP is installed
- cURL to Python: Python requests library
- cURL to Go: Go net/http standard library
- cURL to Ruby: Ruby Net::HTTP snippets
- cURL to Fetch: JavaScript fetch() API
Step-by-Step Tutorial
- Copy your cURL command — e.g. from browser DevTools → Network tab → right-click request → Copy as cURL
- Paste into the converter:
curl -X POST https://api.example.com/v1/send -H "Authorization: Bearer sk-abc123" -H "Content-Type: application/json" -d '{"message":"hello"}' - Select PHP as the target language
- Copy the generated PHP cURL snippet and paste into your script or controller
- Run with
php script.phpor integrate into your Laravel/Symfony/WordPress code
Example Conversion
Input cURL:
curl -X POST https://api.example.com/messages \
-H "Authorization: Bearer mytoken" \
-H "Content-Type: application/json" \
-d '{"text":"hello"}'
Output PHP:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.example.com/messages');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer mytoken',
'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"text":"hello"}');
$response = curl_exec($ch);
curl_close($ch);
echo $response;
Privacy and Data Handling
All conversion runs locally in your browser. cURL commands containing API keys, tokens, or request payloads never leave your device. Safe to use with development credentials — never paste production secrets into shared or cloud-based tools.
Frequently Asked Questions
How do I check for cURL errors in PHP?
After curl_exec(), check curl_errno($ch) — a non-zero value indicates an error. Use curl_error($ch) to get a human-readable error message. Also check the HTTP status code with curl_getinfo($ch, CURLINFO_HTTP_CODE) before parsing the response. Always call curl_close($ch) in a finally block or after the check to free resources. For production code, also set CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT to prevent hanging.
Should I use PHP cURL or Guzzle for API requests?
PHP's native cURL works everywhere without additional dependencies. Guzzle adds a cleaner API, middleware support, async requests, and better error handling — but requires Composer. For simple scripts or WordPress plugins, native cURL is fine. For Laravel, Symfony, or any Composer-based project, Guzzle (or Laravel's HTTP client which wraps Guzzle) is the recommended choice. The converter outputs native cURL which you can adapt to Guzzle syntax with minimal changes.
How do I send JSON data in a PHP cURL request?
Set CURLOPT_POSTFIELDS to the JSON string and add the Content-Type: application/json header. Do not pass an array to CURLOPT_POSTFIELDS for JSON — PHP will encode it as form data instead. Use json_encode($data) to convert a PHP array to a JSON string first: curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['key' => 'value'])).