Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

curl_exec

Executes a cURL request.

Syntax

curl_exec(CurlHandle $handle): string|bool

Parameters

ParameterTypeDescription
$handleCurlHandleA 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

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.

Thanks for reading!

Donate to the site — we work hard for you.