← All tools

cURL to PHP

Turn any curl command into PHP code using Guzzle.
Try
POSThttps://api.example.com/v1/customers
HEADERS · 2
AuthorizationBearer sk_t••••••→ API_TOKEN
Content-Typeapplication/json
BODY · JSON
{
  "email": "ada@example.com",
  "name": "Ada Lovelace",
  "metadata": {
    "plan": "pro",
    "seats": 3
  }
}
PHPGuzzle
<?php
require 'vendor/autoload.php';

$client = new GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.example.com/v1/customers', [
    'headers' => [
        'Authorization' => 'Bearer ' . getenv('API_TOKEN'),
    ],
    'json' => [
        'email' => 'ada@example.com',
        'name' => 'Ada Lovelace',
        'metadata' => [
            'plan' => 'pro',
            'seats' => 3,
        ],
    ],
]);

echo $response->getBody();
20 linesReads secrets with getenv()
● Parsed in this tab-X -H -d --json -F -u -b -G -L -k -m and moreTokens and keys are never sent

How curl maps to Guzzle

The request options you'll see.

  • 'json'A JSON body becomes the json option, which also sets the Content-Type header.
  • 'form_params' · 'multipart'-d form fields use form_params; -F uploads use multipart with fopen().
  • 'auth' · 'timeout'-u becomes ['user', 'pass']. -m 30 becomes 'timeout' => 30.
  • Installcomposer require guzzlehttp/guzzle — the code expects Composer's autoloader.

Questions

The short answers.

Why Guzzle and not curl_init?

Guzzle reads much more clearly and handles JSON, forms and errors for you. The PHP curl extension works too, with more code.

Does Guzzle throw on 4xx/5xx?

Yes, by default. Set 'http_errors' => false to read the status yourself.

Laravel?

Laravel's Http facade wraps Guzzle — the same options apply.

Where do tokens and passwords go?

With “Secrets as env variables” on, they're read with getenv(), so they stay out of your code.

Are large numbers in a JSON body kept?

Yes. If a number wouldn't survive PHP's float, the body is sent as the original JSON text with 'body' instead of 'json', so no digits are lost.

See all →