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:
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:
Example of HTTP requests with file_get_contents, and getting the response code in PHP
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.