What are some alternatives to using the file() function to read server responses in PHP?
Using the file() function in PHP to read server responses can be limited in terms of flexibility and control. An alternative approach is to use cURL, which provides more options for handling HTTP requests and responses.
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, 'http://example.com/api');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute cURL session and store response
$response = curl_exec($ch);
// Close cURL session
curl_close($ch);
// Output the server response
echo $response;