-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurl.php
185 lines (170 loc) · 4.08 KB
/
Curl.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Wrapper for cURL functions
* Uses cURL extension
* @author Khandar William
*/
class Curl
{
/**
* Information about last HTTP request
* @var array contains 'header' and 'content' key
*/
protected $_lastRequest;
/**
* Information about last HTTP response
* @var array contains 'header' and 'content' key
*/
protected $_lastResponse;
/**
* Information about last cURL transfer
* @var array same as curl_getinfo()
*/
protected $_lastInfo;
/**
* Options to be used in curl_setopt_array()
* @var array
*/
protected $_options;
/**
* The cURL resource handle
* @var resource
*/
protected $_curl;
/**
* Constructor
* @param array $options array of default cURL options
*/
public function __construct($options = null)
{
$this->_lastRequest = array('header' => '', 'content' => '');
$this->_lastResponse = array('header' => '', 'content' => '');
$this->_lastInfo = array();
$this->_options = array(
CURLOPT_HEADER => 1,
CURLINFO_HEADER_OUT => 1,
CURLOPT_RETURNTRANSFER => 1,
);
// merge given options with internal default options
if (is_array($options)) {
$this->_options = $this->_options + $options;
}
$this->_curl = null;
}
/**
* Destructor
*/
public function __destruct() {
if (is_resource($this->_curl)) {
curl_close($this->_curl);
}
}
public function getLastRequest()
{
return $this->_lastRequest;
}
public function getLastRequestHeader()
{
return $this->_lastRequest['header'];
}
public function getLastRequestContent()
{
return $this->_lastRequest['content'];
}
public function getLastResponse()
{
return $this->_lastResponse;
}
public function getLastResponseHeader()
{
return $this->_lastResponse['header'];
}
public function getLastResponseContent()
{
return $this->_lastResponse['content'];
}
public function getOptions()
{
return $this->_options;
}
/**
* Return all or a specific information about last transfer
* @param string $key if NULL then return all information
* @return array return NULL if $key does not exist
*/
public function getLastInfo($key = null)
{
if (is_null($key)) {
return $this->_lastInfo;
}
if (isset($this->_lastInfo[$key])) {
return $this->_lastInfo[$key];
}
return null;
}
/**
* Execute a transfer to given URL (with optional options)
* The result can be accessed via getLastResponse()
* @param string $url destination
* @param array $options to add or override default options JUST for this transfer
*/
public function execute($url, $options = null)
{
$options = $this->_options + (array)$options;
$options[CURLOPT_URL] = $url;
if (!$this->_curl) {
$ch = curl_init();
} else {
$ch = $this->_curl;
}
curl_setopt_array($ch, $options);
$reply = curl_exec($ch);
$info = curl_getinfo($ch);
$this->_curl = $ch;
$this->_lastInfo = $info;
// Assign _lastRequest
if (isset($info['request_header'])) {
$this->_lastRequest['header'] = $this->_parseHeader($info['request_header']);
} else {
$this->_lastRequest['header'] = '';
}
if (isset($options[CURLOPT_POSTFIELDS])) {
$this->_lastRequest['content'] = $options[CURLOPT_POSTFIELDS];
} else {
$this->_lastRequest['content'] = '';
}
// Assign _lastResponse
if (preg_match('/^HTTP/', $reply)) {
$pos = strpos($reply, "\r\n\r\n");
$this->_lastResponse['header'] = $this->_parseHeader(substr($reply, 0, $pos+2));
$this->_lastResponse['content'] = substr($reply, $pos+4);
} else {
$this->_lastResponse['header'] = '';
$this->_lastResponse['content'] = $reply;
}
}
/**
* Parse HTTP header text to associative array
* @param string $header the http header
* @return array
*/
protected function _parseHeader($header)
{
$parsed = array();
$exploded = explode("\r\n", $header);
// First line is special
$parsed[] = array_shift($exploded);
// parse the rest of the lines
foreach ($exploded as $line) {
if (preg_match('/([\w\-]+): (.*)/', $line, $m)) {
$parsed[$m[1]] = $m[2];
}
}
return $parsed;
}
}
/*
$a = new Curl();
$a->execute('http://inception.davepedu.com/');
print_r($a);
*/