Example
curl -X POST https://api.example.com/v1/charges \
-u sk_test_YOUR_KEY: \
-d amount=2000 \
-d currency=usd \
-d 'description=Order #1001'
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.example.com/v1/charges',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
],
CURLOPT_POSTFIELDS => 'amount=2000¤cy=usd&description=Order #1001',
CURLOPT_USERPWD => 'sk_test_YOUR_KEY:',
]);
$response = curl_exec($ch);
if ($response === false) {
throw new RuntimeException(curl_error($ch));
}
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
echo $status, PHP_EOL, $response;
curl flags and their CURLOPT options
| curl | PHP option |
|---|---|
-X PUT | CURLOPT_CUSTOMREQUEST => 'PUT' |
-H 'A: b' | CURLOPT_HTTPHEADER => ['A: b'] |
-d 'a=1' | CURLOPT_POSTFIELDS => 'a=1' (implies POST) |
-F 'f=@x.png' | CURLOPT_POSTFIELDS => ['f' => new CURLFile('x.png')] |
-u user:pass | CURLOPT_USERPWD => 'user:pass' |
--digest | CURLOPT_HTTPAUTH => CURLAUTH_DIGEST |
-L | CURLOPT_FOLLOWLOCATION => true |
-k | CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 0 |
-m 10 | CURLOPT_TIMEOUT => 10 |
--connect-timeout 3 | CURLOPT_CONNECTTIMEOUT => 3 |
--compressed | CURLOPT_ENCODING => '' (all supported encodings) |
-x proxy:3128 | CURLOPT_PROXY => 'proxy:3128' |
-I | CURLOPT_NOBODY => true |
-v | CURLOPT_VERBOSE => true |
Decoding a JSON response
$data = json_decode($response, true, flags: JSON_THROW_ON_ERROR);
if ($status >= 400) {
throw new RuntimeException("HTTP $status: " . ($data['error']['message'] ?? $response));
}
curl_exec returns false only for transport errors (DNS, connection, TLS). A 404 or 500 is still a
successful call, so check the status code as above.
Frequently asked questions
Do I need a library to make HTTP requests in PHP?
No. The cURL extension (ext-curl) ships with almost every PHP installation and is what this converter uses. Its options map one to one onto curl’s command-line flags, because both are built on libcurl. In Laravel or Symfony you can use their HTTP clients or Guzzle instead; the requests are the same.
How do I send JSON with PHP cURL?
Set CURLOPT_POSTFIELDS to the JSON string, for example json_encode($data), and add a Content-Type: application/json header with CURLOPT_HTTPHEADER. If you pass an array to CURLOPT_POSTFIELDS instead, PHP sends multipart/form-data, which is a common mistake.
Array or string for CURLOPT_POSTFIELDS?
A string is sent as is, which is right for URL-encoded forms (http_build_query($fields)) and JSON. An array makes PHP send multipart/form-data, which is right for file uploads with CURLFile, like curl -F. The converter picks the one that matches your curl command.
Should I call curl_close()?
Not anymore. Since PHP 8.0 a cURL handle is an object that is freed automatically, and curl_close() does nothing; it is deprecated as of PHP 8.5. The generated code leaves it out.
How do I see what PHP cURL is sending?
Set CURLOPT_VERBOSE => true to print the request and response headers to stderr, like curl -v. To capture request headers in code, set CURLINFO_HEADER_OUT => true and read curl_getinfo($ch, CURLINFO_HEADER_OUT) after curl_exec.