From surenkid, 1 Year ago, written in PHP.
Embed
  1. public function getRemoteFileHeaders($url)
  2.     {
  3.     $options = array(
  4.         CURLOPT_HEADER => true,
  5.         CURLOPT_NOBODY => true,
  6.         CURLOPT_RETURNTRANSFER => true,
  7.         CURLOPT_FOLLOWLOCATION => true,
  8.         CURLOPT_AUTOREFERER => true,
  9.         CURLOPT_TIMEOUT => 30,
  10.         CURLOPT_HTTPHEADER => array('Accept: */*', 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', 'Connection: Keep-Alive')
  11.     );
  12.     $ch = curl_init($url);
  13.     curl_setopt_array($ch, $options);
  14.     $header = curl_exec($ch);
  15.     $ret = curl_errno($ch);
  16.     $error = curl_error($ch);
  17.     curl_close($ch);
  18.     if ($ret === 0) {
  19.         $head = array();
  20.         $headArray = explode("\r\n", trim($header));
  21.         $first = array_shift($headArray);
  22.         preg_match("#HTTP/[0-9\.]+\s+([0-9]+)#", $first, $code);
  23.         $head['code'] = intval($code[1]);
  24.         foreach ($headArray as $v) {
  25.         $arr = explode(':', $v, 2);
  26.         $head[trim($arr[0])] = trim($arr[1]);
  27.         }
  28.         return $head;
  29.     } else {
  30.         return $error;
  31.     }
  32. }