-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.php
54 lines (51 loc) · 1.66 KB
/
util.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
<?php
function curl_post($url, array $post = NULL, array $options = array())
{
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
CURLOPT_POSTFIELDS => http_build_query($post)
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $response = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $response;
}
function http_parse($res){
$parsed = array(
"headers" => array(),
"body" => ""
);
preg_match("/(.*)\r?\n\r?\n(.*)/s", $res, $matches);
$headers = preg_split("/\r?\n|\r/", $matches[1]);
foreach ($headers as $value) {
$header = preg_split("/: /", $value);
if(count($header) == 2){
$header_key = strtolower($header[0]);
switch($header_key){
case "set-cookie":
if(!array_key_exists("Set-Cookie", $parsed['headers'])){
$parsed['headers']['Set-Cookie'] = array();
}
preg_match_all("/([^;\s]+)=([^;]*)/s", $header[1], $values);
$cookie_name = $values[1][0];
$values[1][0] = "value";
$parsed['headers']['Set-Cookie'][$cookie_name] = array_combine($values[1], $values[2]);
break;
default:
$parsed['headers'][$header_key] = $header[1];
}
}
}
$parsed['body'] = $matches[2];
return $parsed;
}