Skip to content

Instantly share code, notes, and snippets.

@plopesc
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plopesc/10a7c46cedca0f84944c to your computer and use it in GitHub Desktop.
Save plopesc/10a7c46cedca0f84944c to your computer and use it in GitHub Desktop.
Get Header from URL
<?php
/**
* To execute this: php headers.php http.//url_to_check.com header
*/
$strURL = isset($argv[1]) ? $argv[1] : NULL;
$headerFind = isset($argv[2]) ? $argv[2] : NULL;
$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);
if (isset($headerFind)) {
$strHeaders2 = explode("\n", str_replace("\r", '', $strHeaders));
$headers = array();
foreach ($strHeaders2 as $header) {
$value = explode(': ', $header);
if (isset ($value[0]) && isset($value[1])) {
$headers[$value[0]] = $value[1];
}
}
echo isset($headers[$headerFind]) ? $headers[$headerFind] : 'Header not found';
}
else {
echo $strHeaders;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment