curl_setopt
Sets an option of a cURL request.
Syntax
curl_setopt(CurlHandle $handle, int $option, mixed $value): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $handle | CurlHandle | A request created with `curl_init()`. |
| $option | int | The option — a `CURLOPT_…` constant. |
| $value | mixed | The option value. |
Return value
bool`true` on success.
Examples
Sending JSON with POST:
<?php
// create-order.php
$curl = curl_init('https://api.example.com/orders');
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(['product_id' => 42]),
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
]);
$response = curl_exec($curl);Notes
Several options are easier to set with one curl_setopt_array() call. Always set CURLOPT_TIMEOUT.
See also
- curl_init — Creates a cURL request.
- curl_exec — Executes a cURL request.
- http_build_query — Builds a URL query string from an array.
Related recipes
In the tutorial
- Calling other sites — How to call someone else's API from PHP — file_get_contents, the cURL extension, sending JSON, headers, the response code and error handling.