Skip to content

Instantly share code, notes, and snippets.

@neopunisher
Last active October 3, 2017 14:45
Show Gist options
  • Save neopunisher/5292506 to your computer and use it in GitHub Desktop.
Save neopunisher/5292506 to your computer and use it in GitHub Desktop.
Parse the http headers that curl returns raw
<?
$sUrl = 'http://cartercole.com';
$oCurlConnection = curl_init();
curl_setopt($oCurlConnection, CURLOPT_FAILONERROR, true); // If error code found, fail connection
curl_setopt($oCurlConnection, CURLOPT_URL,$sUrl); // Url to request
//curl_setopt($oCurlConnection, CURLOPT_USERAGENT, $sUserAgent); // User Agent of request
curl_setopt($oCurlConnection, CURLOPT_CONNECTTIMEOUT, 30); // Time out for a single connection
curl_setopt($oCurlConnection, CURLOPT_TIMEOUT, 60); // Curl Process Timeout
curl_setopt($oCurlConnection, CURLOPT_FOLLOWLOCATION, true); // Follow Redirects If 302
curl_setopt($oCurlConnection, CURLOPT_MAXREDIRS, 30); // Max Redirects Allowed
curl_setopt($oCurlConnection, CURLOPT_HEADER, TRUE);
curl_setopt($oCurlConnection, CURLOPT_RETURNTRANSFER, true); // Return response
//curl_setopt($oCurlConnection, CURLOPT_HTTPHEADER, $header); // Accept html in western or utf-8
//curl_setopt($oCurlConnection, CURLOPT_HTTPHEADER, array("Accept: text/html","Accept-Charset: ISO-8859-1,utf-8")); // Accept html in western or utf-8
$result = curl_exec($oCurlConnection);
$aHeaderInfo = curl_getinfo($oCurlConnection);
$curlHeaderSize=$aHeaderInfo['header_size'];
$sBody = trim(mb_substr($result, $curlHeaderSize));
$ResponseHeader = explode("\n",trim(mb_substr($result, 0, $curlHeaderSize)));
unset($ResponseHeader[0]);
$aHeaders = array();
foreach($ResponseHeader as $line){
list($key,$val) = explode(':',$line,2);
$aHeaders[strtolower($key)] = trim($val);
}
echo '<pre>';
var_dump($aHeaders);
echo "\n\n\n\n\n";
var_dump(htmlentities ( $sBody));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment