-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.php
More file actions
151 lines (138 loc) · 3.99 KB
/
HttpRequest.php
File metadata and controls
151 lines (138 loc) · 3.99 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace X\Service\QQConnect;
/**
* Http request handler class.
*/
class HttpRequest {
/**
* parameters to current request.
* @var array
*/
private $parameters = array();
/**
* requsted url.
* @var string
*/
private $url = null;
/**
* init current instance.
* @param string $url
* @param array $parms
*/
public function __construct( $url, $parms=array()) {
$this->url = $url;
$this->parameters = $parms;
}
/**
* convert current requst to string.
* @return string
*/
public function toString() {
$parms = array();
foreach ( $this->parameters as $key=>$value ) {
$parms[] = sprintf('%s=%s', $key, $value);
}
$connector = ( false === strpos($this->url, '?') ) ? '?' : '&';
$url = $this->url.$connector.implode('&', $parms);
return $url;
}
/**
* This value saved the response text.
* @var string
*/
private $response = null;
/**
* Execute get requst and parse response.
* @param string $format
* @param boolean $refresh
* @return mixed
*/
public function get( $format='', $refresh=false ) {
if ( null === $this->response || $refresh ) {
$combined = $this->toString();
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $combined);
$this->response = curl_exec($ch);
curl_close($ch);
}
return $this->formatResponse($this->response, $format);
}
/**
* Execute post requst and parse response.
* @param string $format
* @param boolean $refresh
* @return mixed
*/
public function post( $format='', $refresh=false) {
if ( null === $this->response || $refresh ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->parameters);
curl_setopt($ch, CURLOPT_URL, $this->url);
$this->response = curl_exec($ch);
curl_close($ch);
}
return $this->formatResponse($this->response, $format);
}
/**
* format response string by given formation.
* @param string $response
* @param string $format
* @return mixed
*/
private function formatResponse( $response, $format='' ) {
$handler = sprintf('formatResponse%s', $format);
if ( ''!==$format && method_exists($this, $handler) ) {
return $this->$handler($response);
} else {
return $response;
}
}
/**
* get data from json string.
* @param string $response
* @return array
*/
private function formatResponseJSON( $response ) {
return json_decode($response, true);
}
/**
* get data from url parameter string.
* @param string $response
* @return multitype:
*/
private function formatResponseURLParam( $response ) {
$params = array();
parse_str($response, $params);
return $params;
}
/**
* get data from javascript callback string.
* @param string $response
* @return multitype:
*/
private function formatResponseJSCallbackJSON( $response ) {
$response = substr($response, 10);
$response = substr($response, 0, strlen($response)-4);
$response = json_decode($response, true);
return $response;
}
/**
* fromat data from json.
* @var string
*/
const FOTMAT_JSON = 'JSON';
/**
* format data from url parameter.
* @var string
*/
const FORMAT_URL_PARAM = 'URLParam';
/**
* format data from js callback string.
* @var string
*/
const FORMAT_JS_CALLBACK_JSON = 'JSCallbackJSON';
}