What are best practices for handling HTTP responses in PHP when making requests?

When making HTTP requests in PHP, it is important to properly handle the responses to ensure that the request was successful and to process any data returned. One common practice is to check the HTTP status code of the response to determine if the request was successful (status code 200). Additionally, it is recommended to handle any errors or exceptions that may occur during the request.

// Make an HTTP request
$response = file_get_contents('https://api.example.com/data');

// Check if the request was successful
if ($response === false) {
    // Handle any errors
    echo "Error: Unable to retrieve data.";
} else {
    // Process the response data
    $data = json_decode($response, true);
    // Do something with the data
}