cURL to PHP

Convert curl to PHP with the cURL extension: real output, a multipart upload example and SSL checks.

loading cURL to PHP
About this tool

cURL to PHP: from curl to code

Paste a curl command and get PHP that makes the same request with the cURL extension: curl_init, curl_setopt_array with CURLOPT_URL, CURLOPT_CUSTOMREQUEST, CURLOPT_HTTPHEADER and CURLOPT_POSTFIELDS, then curl_exec with an error check. PHP's cURL extension is a binding of the same libcurl, so almost every flag has a direct option.

This page opens the converter on PHP cURL. The command is only read and turned into text: nothing is run and nothing is sent from the page.

A real conversion

The tool opens with this command, a POST of a JSON order with a bearer token:

curl -X POST 'https://api.example.com/v1/orders?expand=items' \
  -H 'Authorization: Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc' \
  -H 'Content-Type: application/json' \
  -d '{"customer":"ana@example.com","items":[{"sku":"KB-01","qty":2}],"coupon":null}'

and this is what it writes for PHP cURL:

<?php

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.example.com/v1/orders?expand=items',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc',
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => '{"customer":"ana@example.com","items":[{"sku":"KB-01","qty":2}],"coupon":null}',
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new RuntimeException(curl_error($ch));
}
echo curl_getinfo($ch, CURLINFO_HTTP_CODE), "\n", $response;
curl_close($ch);

Which flags become what

Starting from a plain GET of https://api.example.com/items, each flag below adds this to the generated code (computed with the converter itself):

FlagWhat it isFirst new line
-H 'Accept: application/json'a request headerCURLOPT_HTTPHEADER => [
-d 'a=1&b=2'a body (the method becomes POST)CURLOPT_CUSTOMREQUEST => 'POST',
-u ana:secretbasic authenticationCURLOPT_USERPWD => 'ana:secret',
-F 'photo=@a.png'a multipart file uploadCURLOPT_CUSTOMREQUEST => 'POST',
-X DELETEanother methodCURLOPT_CUSTOMREQUEST => 'DELETE',

Another command

curl https://api.example.com/upload -F 'file=@photo.png' -F 'title=Holiday'
<?php

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.example.com/upload',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => [
        'file' => new CURLFile('photo.png'),
        'title' => 'Holiday',
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new RuntimeException(curl_error($ch));
}
echo curl_getinfo($ch, CURLINFO_HTTP_CODE), "\n", $response;
curl_close($ch);

A multipart upload: -F fields go to CURLOPT_POSTFIELDS as an array, and a file is wrapped in CURLFile. Passing an array makes cURL send multipart/form-data by itself.

Running it

php -m | grep curl      # the extension must be listed
php request.php

Common mistakes

  • Disabling certificate checks (CURLOPT_SSL_VERIFYPEER false) to silence an error: that removes the protection of HTTPS. Fix the CA bundle (curl.cainfo in php.ini) instead.
  • Sending a JSON string without the Content-Type header: many APIs then read it as a form. The tool copies the header from your command; keep it.

How to use the converter

  1. Copy the command: from the browser (network tab, Copy as cURL), from an API reference or from your terminal history.
  2. Paste it in the box; lines that end with a backslash are joined, so a multi-line command works.
  3. Copy the code or download it as a file.

Frequently asked questions

How do I convert curl to PHP?

Paste the command and copy the PHP; run it with the cURL extension enabled, which is the default in most installations.

Should I use Guzzle instead?

For one request the extension is enough and has no dependency. Guzzle adds a nicer API, promises and middleware, which pay off in larger projects.

How do I read the response as JSON?

Use json_decode($response, true) on the string curl_exec returns; check json_last_error() when the API may answer with HTML on an error.

Report a problem