forked from phpnode/YiiCurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACurlResponse.php
executable file
·155 lines (144 loc) · 3.94 KB
/
ACurlResponse.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
<?php
/**
* Holds a curl response.
* @author Charles Pick
* @package packages.curl
*/
class ACurlResponse extends CComponent {
/**
* The ACurl object that owns this curl response.
* @var ACurl
*/
public $request;
/**
* The actual data returned by curl.
* @var string
*/
public $data;
/**
* Holds the information returned by the CURL request.
* @see getInfo()
* @see setInfo()
* @var CAttributeCollection
*/
protected $_info;
/**
* Holds the header information returned by the CURL request.
* @see getHeaders()
* @see setHeaders()
* @var CTypedList
*/
protected $_headers;
/**
* Returns information about the CURL response.
* @return CAttributeCollection the curl response details
*/
public function getInfo() {
if ($this->_info === null) {
$this->_info = new CAttributeCollection(curl_getinfo($this->request->getHandle()),true);
}
return $this->_info;
}
/**
* Sets the curl response details for this request.
* @param array $value The array for values from curl_getinfo
* @return ACurlResponse $this with the response details applied
*/
public function setInfo($value) {
if (is_array($value)) {
$this->_info = new CAttributeCollection($value,true);
}
elseif ($value instanceof CAttributeCollection) {
$this->_info = $value;
}
else {
throw new CException("ACurl::setInfo() - \$value must be an array or a CAttributeCollection");
}
}
/**
* Gets the last headers returned by the response
* @return CAttributeCollection|boolean the headers or false if there were no headers returned
*/
public function getLastHeaders() {
if (!$this->_headers)
return false;
if ($this->getHeaders()->count() > 0)
return $this->getHeaders()->itemAt(0);
return false;
}
/**
* Gets the headers for this request.
* @return CTypedList a typed list of attribute
* collections representing each header.
*/
public function getHeaders() {
return $this->_headers;
}
/**
* Sets the headers for this response.
* @param mixed $headers The headers to set, can be a string or an array
* @return ACurlResponse $this with the header details applied
*/
public function setHeaders($headers) {
$this->_headers = new CTypedList("CAttributeCollection");
if (is_string($headers)) {
foreach(preg_split("/(\r\n){2,2}/",$headers,2) as $header) {
if (trim($header) == "") {
continue;
}
$item = array();
$lines = explode("\r\n",$header);
foreach($lines as $n => $line) {
if ($n == 0) {
$line = explode(" ",$line);
$item['http_code'] = (int) trim($line[1]);
}
else {
$key = mb_substr($line,0,mb_strpos($line,":"));
if ($key == "") {
continue;
}
$value = trim(mb_substr($line,mb_strlen($key) + 1));
$item[$key] = $value;
}
}
$this->_headers[] = new CAttributeCollection($item, true);
}
}
elseif (is_array($headers)) {
foreach($headers as $header) {
$this->_headers[] = new CAttributeCollection($header, true);
}
}
return $this; // chainable
}
/**
* Returns the response data when the object is cast to a string
* @return string The response data
*/
public function __toString() {
return $this->data;
}
/**
* Decodes a JSON response
* @param boolean $forceArray whether to force the returned object to be an array or not
* @return mixed The decoded JSON object
*/
public function fromJSON($forceArray = true) {
if (function_exists("json_decode")) {
return json_decode($this->data, $forceArray);
}
else {
return CJSON::decode($this->data, $forceArray);
}
}
/**
* Determines whether this response is a HTTP error or not.
* @return boolean true if it's an error
*/
public function getIsError() {
if($this->getInfo()->contains('http_code'))
return $this->getInfo()->http_code > 399;
return true;
}
}