Skip to content

Instantly share code, notes, and snippets.

Created April 5, 2013 04:08
Show Gist options
  • Save anonymous/5316554 to your computer and use it in GitHub Desktop.
Save anonymous/5316554 to your computer and use it in GitHub Desktop.
Read the values from a cURL response header using regex.
<?php
define('COOKIE_FILE', 'cookie.txt');
define('HEADER_REGEX', '#^.*?(\r?\n){2}#msi');
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $assetURL,
CURLOPT_POST => FALSE,
CURLOPT_HEADER => TRUE,
CURLOPT_BINARYTRANSFER => FALSE,
// CURLOPT_ENCODING => 'identity',
// CURLOPT_NOBODY => TRUE,
CURLOPT_COOKIESESSION => FALSE,
CURLOPT_COOKIEJAR => COOKIE_FILE,
CURLOPT_COOKIEFILE, COOKIE_FILE,
CURLOPT_RETURNTRANSFER => TRUE
));
$assetResult = curl_exec($ch);
curl_close($ch);
unlink(COOKIE_FILE);
preg_match(HEADER_REGEX, $assetResult, $headerMatch);
$assetResult = str_replace($headerMatch[0], '', $assetResult);
$ignoreHeadersRegex = '#' . implode('|', $ignoreHeaders) . '#msi';
$headers = preg_split('#\r?\n#', $headerMatch[0]);
foreach ($headers as $header) {
if (!preg_match($ignoreHeadersRegex, $header)) {
header($header);
// echo $header;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment