Recent Posts
- 10 Things To Do When Launching A Site
- Using PHP to connect to SFTP
- Asynchronous Virtual Pageviews with Google Analytics
- 5 Advanced Text Editing Keyboard Shortcuts
- Get the headers of a HTTP request with PHP
- How-to: Create PDF preview images in PHP – Part 2
- How-to: Create PDF preview images in PHP
- Quick Code: Get the domain name in JS
- Things to think about when designing a logo
- Javascript Array Functions
Topics
Get the headers of a HTTP request with PHP
Posted on 20th January 2011 by Jacob Wyke
Need to check that a URL is valid? Or get the last-modified date, or the charset, or the ETag of a page? Here is a simple bit of code to help you do just that by returning the headers of a HTTP request for you.
The following PHP code is the equivalent of ‘curl -I‘:
<?php
$strURL = "www.webvamp.co.uk";
$resCurl = curl_init();
//set URL and other appropriate options
curl_setopt($resCurl, CURLOPT_URL, $strURL);
curl_setopt($resCurl, CURLOPT_HEADER, true);
curl_setopt($resCurl, CURLOPT_NOBODY, true);
curl_setopt($resCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($resCurl, CURLOPT_FOLLOWLOCATION, true);
//get the headers
$strHeaders = curl_exec($resCurl);
//close cURL
curl_close($resCurl);
echo $strHeaders;
?>
You will of course need the cURL library compiled with PHP.
Be the first to add your voice to this post!
Start the discussion and leave your views or thoughts by writing a comment using the form above.