getheadheaders
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
define('HTTP_MAX_REDIRECT_COUNT', 5);
define('HTTP_MAX_HEADER_LENGTH', 2560);
define('HTTP_DEFAULT_PORT', 80);
define('HTTP_TIMEOUT', 5);
function get_head_headers($url, $follow_redirect = false) {
static $redirect_count;
if(!is_array($url)) {
$url = parse_url($url);
}
if(!array_key_exists('port', $url)) {
$url['port'] = HTTP_DEFAULT_PORT;
}
if(empty($url['path'])) {
$url['path'] = '/';
}
$socket = @fsockopen($url['host'], $url['port'], $error_no, $error_str, HTTP_TIMEOUT);
if(!$socket) {
throw new LogicException("Could not create connection. Reason: $error_str");
}
$request = sprintf("HEAD %s HTTP/1.1\r\nHost: %s\r\nConnection: Close\r\n\r\n",
$url['path'] . (isset($url['query']) ? '?' . $url['query'] : ''),
$url['host']);
if(!fwrite($socket, $request)) {
throw new LogicException("Could not write to socket. Reason: $error_str");
}
$headers = array();
while(!feof($socket)) {
list($name, $value) = array_merge(explode(':', fgets($socket, HTTP_MAX_HEADER_LENGTH), 2), array(''));
if(!trim($name)) continue;
if($value) {
$headers[trim($name)] = trim($value);
} else {
$headers[] = trim($name); // voor de 1e 200 OK regel
}
}
fclose($socket);
if($follow_redirect && array_key_exists('Location', $headers)) {
if($redirect_count++ >= HTTP_MAX_REDIRECT_COUNT) {
throw new LogicException('Max redirect count reached');
}
$new_url = parse_url($headers['Location']);
if(!isset($new_url['host'])) {
$new_url['host'] = $url['host'];
}
if($new_url['path'][0] != '/') {
$new_url['path'] = dirname($url['path']) . '/' . $new_url['path'];
}
return get_head_headers($new_url, true);
}
$redirect_count = 0;
return $headers;
}
?>
define('HTTP_MAX_REDIRECT_COUNT', 5);
define('HTTP_MAX_HEADER_LENGTH', 2560);
define('HTTP_DEFAULT_PORT', 80);
define('HTTP_TIMEOUT', 5);
function get_head_headers($url, $follow_redirect = false) {
static $redirect_count;
if(!is_array($url)) {
$url = parse_url($url);
}
if(!array_key_exists('port', $url)) {
$url['port'] = HTTP_DEFAULT_PORT;
}
if(empty($url['path'])) {
$url['path'] = '/';
}
$socket = @fsockopen($url['host'], $url['port'], $error_no, $error_str, HTTP_TIMEOUT);
if(!$socket) {
throw new LogicException("Could not create connection. Reason: $error_str");
}
$request = sprintf("HEAD %s HTTP/1.1\r\nHost: %s\r\nConnection: Close\r\n\r\n",
$url['path'] . (isset($url['query']) ? '?' . $url['query'] : ''),
$url['host']);
if(!fwrite($socket, $request)) {
throw new LogicException("Could not write to socket. Reason: $error_str");
}
$headers = array();
while(!feof($socket)) {
list($name, $value) = array_merge(explode(':', fgets($socket, HTTP_MAX_HEADER_LENGTH), 2), array(''));
if(!trim($name)) continue;
if($value) {
$headers[trim($name)] = trim($value);
} else {
$headers[] = trim($name); // voor de 1e 200 OK regel
}
}
fclose($socket);
if($follow_redirect && array_key_exists('Location', $headers)) {
if($redirect_count++ >= HTTP_MAX_REDIRECT_COUNT) {
throw new LogicException('Max redirect count reached');
}
$new_url = parse_url($headers['Location']);
if(!isset($new_url['host'])) {
$new_url['host'] = $url['host'];
}
if($new_url['path'][0] != '/') {
$new_url['path'] = dirname($url['path']) . '/' . $new_url['path'];
}
return get_head_headers($new_url, true);
}
$redirect_count = 0;
return $headers;
}
?>