1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class cHttpRequestCurl extends cHttpRequest {
25:
26: 27: 28: 29: 30:
31: protected $curl;
32:
33: 34: 35: 36: 37:
38: protected $postArray;
39:
40: 41: 42: 43: 44:
45: protected $getArray;
46:
47: 48: 49: 50: 51:
52: protected ;
53:
54: 55: 56: 57: 58:
59: protected $url;
60:
61: 62: 63: 64: 65: 66: 67:
68: public function __construct($url = '') {
69: $this->curl = curl_init(($url == '') ? NULL : $url);
70: $this->setURL($url);
71: }
72:
73: 74: 75: 76:
77: public function setGetParams($array) {
78: $this->getArray = $array;
79:
80: return $this;
81: }
82:
83: 84: 85: 86:
87: public function setPostParams($array) {
88: $this->postArray = $array;
89:
90: return $this;
91: }
92:
93: 94: 95: 96:
97: public function ($array) {
98: $this->headerArray = $array;
99:
100: return $this;
101: }
102:
103: 104: 105: 106:
107: public function setURL($url) {
108: $this->url = $url;
109:
110: return $this;
111: }
112:
113: 114: 115:
116: protected function preparePostRequest() {
117: if (is_array($this->postArray)) {
118: $this->setOpt(CURLOPT_POST, 1);
119: $this->setOpt(CURLOPT_POSTFIELDS, $this->postArray);
120: }
121: }
122:
123: 124: 125:
126: protected function prepareGetRequest() {
127: if (is_array($this->getArray)) {
128: if (!cString::contains($this->url, '?')) {
129: $this->url .= "?";
130: } else {
131: $this->url .= '&';
132: }
133: foreach ($this->getArray as $key => $value) {
134: $this->url .= urlencode($key) . '=' . urlencode($value) . '&';
135: }
136: $this->url = substr($this->url, 0, strlen($this->url) - 1);
137: }
138: $this->setOpt(CURLOPT_URL, $this->url);
139: }
140:
141: 142: 143:
144: protected function () {
145: $curlHeaderArray = array();
146: if (!is_array($this->headerArray)) {
147: return;
148: }
149: foreach ($this->headerArray as $key => $value) {
150: $headerString = '';
151: if (is_array($value)) {
152: $headerString .= $value[0] . ': ' . $value[1];
153: } else {
154: $headerString .= $key . ': ' . $value;
155: }
156: array_push($curlHeaderArray, $headerString);
157: }
158:
159: $this->setOpt(CURLOPT_HTTPHEADER, $curlHeaderArray);
160: }
161:
162: 163: 164: 165: 166: 167: 168: 169:
170: protected function sendRequest($return, $method, $returnHeaders) {
171: $this->setOpt(CURLOPT_RETURNTRANSFER, true);
172: $this->setOpt(CURLOPT_HEADER, true);
173: $this->setOpt(CURLOPT_URL, $this->url);
174:
175: $this->prepareHeaders();
176: $this->prepareGetRequest();
177: if ($method = 'POST') {
178: $this->preparePostRequest();
179: }
180:
181: $string = curl_exec($this->curl);
182:
183: if ($return) {
184: if (!$returnHeaders) {
185: $string = substr(cString::strstr($string, "\r\n\r\n"), strlen("\r\n\r\n"));
186: }
187: return $string;
188: } else {
189: return strpos(cString::strstr($string, "\r\n", true), '200') !== false || strpos(cString::strstr($string, "\r\n", true), '100') !== false;
190: }
191: }
192:
193: 194: 195: 196:
197: public function postRequest($return = true, $returnHeaders = false) {
198: return $this->sendRequest($return, 'POST', $returnHeaders);
199: }
200:
201: 202: 203: 204:
205: public function getRequest($return = true, $returnHeaders = false) {
206: return $this->sendRequest($return, 'GET', $returnHeaders);
207: }
208:
209: 210: 211: 212:
213: public function request($return = true, $returnHeaders = false) {
214: return $this->sendRequest($return, 'POST', $returnHeaders);
215: }
216:
217: 218: 219: 220: 221: 222: 223:
224: public function setOpt($curlOpt, $value) {
225: curl_setopt($this->curl, $curlOpt, $value);
226:
227: return $this;
228: }
229:
230: 231: 232: 233: 234:
235: public function getCurl() {
236: return $this->curl;
237: }
238: }
239:
240: ?>
241: