How can curl be used to send a request to an API with PHP?

To send a request to an API with PHP using cURL, you can use the cURL functions in PHP to make an HTTP request to the API endpoint. You need to set the appropriate options such as the URL, request method, headers, and data to be sent in the request. Finally, you can execute the cURL request and handle the response as needed.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.example.com/endpoint');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['param1' => 'value1', 'param2' => 'value2']));

$response = curl_exec($ch);

if($response === false){
    echo 'cURL error: ' . curl_error($ch);
} else {
    echo 'Response: ' . $response;
}

curl_close($ch);