What are some alternative methods for retrieving response headers in PHP?
When making HTTP requests in PHP, you may need to retrieve response headers for various reasons such as checking the status of the request or extracting specific information. One common method to retrieve response headers is by using the `get_headers()` function. However, an alternative method is to use the `curl_getinfo()` function from the cURL library, which provides more detailed information about the request.
// Using cURL to retrieve response headers
$url = 'https://www.example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
// Get response headers
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
// Close cURL session
curl_close($ch);
echo "HTTP Code: $httpCode\n";
echo "Content Type: $contentType\n";
Related Questions
- What is the significance of returning the parent::query($str) in the jMySQLi class constructor and how does it affect query execution?
- What are the potential pitfalls of using foreach loops in PHP when working with arrays?
- Are there any best practices for handling Excel files in PHP, especially when dealing with client requirements?