-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mfwHttp.php
132 lines (114 loc) · 3.31 KB
/
mfwHttp.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
class mfwHttp {
protected static function initialize_curl($url,$headers,$timeout)
{
$curl = curl_init($url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_HEADER,true);
if(!empty($headers)){
curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
}
if($timeout){
curl_setopt($curl,CURLOPT_TIMEOUT,$timeout);
}
$proxy = mfwServerEnv::httpProxy();
if($proxy){
curl_setopt($curl,CURLOPT_PROXY, $proxy['host']);
curl_setopt($curl,CURLOPT_PROXYPORT, $proxy['port']);
}
return $curl;
}
protected static function exec($curl,&$response)
{
$ret = curl_exec($curl);
if(!$ret){
$response['status'] = 0;
$response['status_msg'] = '';
$response['headers'] = array();
return null;
}
list($headers,$body) = explode("\r\n\r\n",$ret,2);
if(preg_match('|^HTTP/[0-9\.]+ 200 Connection Established|',$headers)){
// HTTPSの時はCONNECTのヘッダを除外
list($headers,$body) = explode("\r\n\r\n",$body,2);
}
$headers = explode("\r\n",$headers);
$status = explode(' ', array_shift($headers), 3);
$response['status'] = (int)$status[1];
$response['status_msg'] = $status[2];
$response['headers'] = $headers;
return $body;
}
public static function get($url,$headers=array(),&$response=null,$timeout=10)
{
$curl = static::initialize_curl($url,$headers,$timeout);
return static::exec($curl,$response);
}
public static function post($url,$body='',$headers=array(),&$response=null,$timeout=10)
{
if(is_array($body)){
$body = static::composeParams($body);
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
}
$curl = static::initialize_curl($url,$headers,$timeout);
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$body);
return static::exec($curl,$response);
}
public static function put($url,$body='',$headers=null,&$response=null,$timeout=10)
{
if(is_array($body)){
$body = static::composeParams($body);
}
$curl = static::initialize_curl($url,$headers,$timeout);
curl_setopt($curl,CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl,CURLOPT_POSTFIELDS,$body);
return static::exec($curl,$response);
}
public static function delete($url,$headers=null,&$response=null,$timeout=10)
{
$curl = static::initialize_curl($url,$headers,$timeout);
curl_setopt($curl,CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
return static::exec($curl,$response);
}
public static function composeParams(Array $params)
{
$p = array();
foreach($params as $k=>$v){
if($k!==''){
$p[] = urlencode($k).'='.urlencode($v);
}
}
return implode('&',$p);
}
public static function composeURL($basefeed,$params)
{
list($url,$baseparams,$anchor) = static::extractURL($basefeed);
$params = static::composeParams(array_merge($baseparams,$params));
if($params){
$url .= "?{$params}";
}
if(!is_null($anchor)){
$url .= "#{$anchor}";
}
return $url;
}
public static function extractURL($url)
{
$u = explode('#',$url);
$anchor = (isset($u[1]))? $u[1]: null;
$u = explode('?',$u[0]);
$base = $u[0];
$params = array();
if(isset($u[1]) && $u[1]!=''){
foreach(explode('&',$u[1]) as $p){
$pp = explode('=',$p);
$k = urldecode($pp[0]);
$v = urldecode($pp[1]);
$params[$k] = $v;
}
}
return array($base,$params,$anchor);
}
}