HTTP requests with file_get_contents, getting the response code

To make an HTTP request using the file_get_contents function in PHP and retrieve the response code, you can use the following code snippet:

$url = "http://example.com";
$context = stream_context_create(array('http' => array('ignore_errors' => true)));
$data = file_get_contents($url, false, $context);
$response_code = substr($http_response_header[0], 9, 3);

This will make a GET request to the specified URL and store the response body in the $data variable. The response code will be stored in the $response_code variable, which is obtained by parsing the first element of the $http_response_header array (which is automatically created by the file_get_contents function when the http stream wrapper is used).

Note that you can use this method to handle redirects and other types of non-200 responses, as file_get_contents automatically follows redirects when the http stream wrapper is used.

Also, This method is not suitable for handling large files, for large file handling we can use curl.