curl_exec
Executes a cURL request.
Syntax
curl_exec(CurlHandle $handle): string|boolParameters
| Parameter | Type | Description |
|---|---|---|
| $handle | CurlHandle | A configured request. |
Return value
string|boolThe response body with `CURLOPT_RETURNTRANSFER`, otherwise `true`; `false` on failure.
Examples
A request with error checking:
<?php
// api.php
$curl = curl_init('https://api.example.com/users/1');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$body = curl_exec($curl);
if ($body === false) {
exit('Request failed: ' . curl_error($curl));
}
$user = json_decode($body, true);Notes
false means a connection failure. A 404 or 500 response is not a failure — check the code with curl_getinfo($curl, CURLINFO_HTTP_CODE).
See also
- curl_init — Creates a cURL request.
- curl_setopt — Sets an option of a cURL request.
- json_decode — Parses a JSON string.
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.