Last active
August 29, 2015 14:16
-
-
Save plopesc/10a7c46cedca0f84944c to your computer and use it in GitHub Desktop.
Get Header from URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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