How can the flexibility of the cURL function be improved to allow for more dynamic URL configurations in PHP?

The flexibility of the cURL function in PHP can be improved by using variables to dynamically configure the URL. By storing the base URL in a variable and appending any dynamic components as needed, the cURL function can be easily adapted to different scenarios without hardcoding the entire URL each time.

// Base URL
$base_url = "https://api.example.com/";

// Dynamic components
$endpoint = "users";
$user_id = 123;

// Construct dynamic URL
$url = $base_url . $endpoint . "/" . $user_id;

// Initialize cURL session
$ch = curl_init($url);

// Set cURL options and execute request
// (example: curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);)
$response = curl_exec($ch);

// Close cURL session
curl_close($ch);

// Handle response data as needed