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 cHttpRequestSocket extends cHttpRequest {
25:
26: 27: 28: 29: 30:
31: protected $postArray;
32:
33: 34: 35: 36: 37:
38: protected $getArray;
39:
40: 41: 42: 43: 44:
45: protected ;
46:
47: 48: 49: 50: 51:
52: protected $url;
53:
54: 55: 56: 57: 58:
59: protected $boundary;
60:
61: 62: 63: 64: 65:
66: protected ;
67:
68: 69: 70: 71: 72:
73: protected $body;
74:
75: 76: 77: 78: 79: 80: 81:
82: public function __construct($url = '') {
83: $this->url = $url;
84: }
85:
86: 87: 88:
89: public function setURL($url) {
90: $this->url = $url;
91:
92: return $this;
93: }
94:
95: 96: 97:
98: public function setGetParams($array) {
99: $this->getArray = $array;
100:
101: return $this;
102: }
103:
104: 105: 106:
107: public function setPostParams($array) {
108: $this->postArray = $array;
109:
110: return $this;
111: }
112:
113: 114: 115:
116: public function ($array) {
117: $this->headerArray = $array;
118:
119: return $this;
120: }
121:
122: 123: 124:
125: protected function () {
126: $this->header = '';
127: if (!is_array($this->headerArray)) {
128: return;
129: }
130: foreach ($this->headerArray as $key => $value) {
131: $headerString = '';
132: if (is_array($value)) {
133: $headerString .= $value[0] . ': ' . $value[1];
134: } else {
135: $headerString .= $key . ': ' . $value;
136: }
137: $this->header .= $headerString . "\r\n";
138: }
139: }
140:
141: 142: 143:
144: protected function prepareGetRequest() {
145: if (is_array($this->getArray)) {
146: if (!cString::contains($this->url, '?')) {
147: $this->url .= '?';
148: } else {
149: $this->url .= '&';
150: }
151: foreach ($this->getArray as $key => $value) {
152: $this->url .= urlencode($key) . '=' . urlencode($value) . '&';
153: }
154: $this->url = substr($this->url, 0, strlen($this->url) - 1);
155: }
156: }
157:
158: 159: 160:
161: protected function preparePostRequest() {
162: $this->boundary = md5(time()) . md5(time() * rand());
163: $this->headerArray['Content-Type'] = 'multipart/form-data; boundary=' . $this->boundary;
164: $this->boundary = '--' . $this->boundary;
165:
166: $this->body = $this->boundary . "\r\n";
167: foreach ($this->postArray as $key => $value) {
168: $this->body .= 'Content-Disposition: form-data; name="' . $key . "\"\r\n\r\n";
169: $this->body .= $value . "\r\n";
170: $this->body .= $this->boundary . "\r\n";
171: }
172: $this->headerArray['Content-Length'] = strlen($this->body);
173: }
174:
175: 176: 177: 178: 179: 180: 181: 182:
183: protected function sendRequest($return, $method, $returnHeaders = false) {
184: if (!(strpos($this->url, 'http') === 0)) {
185: $this->url = 'http://' . $this->url;
186: }
187:
188: $urlInfo = @parse_url($this->url);
189: $scheme = '';
190: if ($urlInfo['port'] == '') {
191: if ($urlInfo['scheme'] == 'https') {
192: $urlInfo['port'] = 443;
193: $scheme = 'ssl://';
194: } else {
195: $urlInfo['port'] = 80;
196: }
197: }
198:
199: $this->headerArray['Host'] = ($this->headerArray['Host'] != '') ? $this->headerArray['Host'] : $urlInfo['host'];
200: $this->headerArray['Connection'] = ($this->headerArray['Connection'] != '') ? $this->headerArray['Host'] : 'close';
201: $this->headerArray['Accept'] = ($this->headerArray['Accept'] != '') ? $this->headerArray['Host'] : '*/*';
202:
203: $this->prepareHeaders();
204:
205: $handle = @fsockopen($scheme . $urlInfo['host'], $urlInfo['port']);
206: if (!$handle) {
207: return false;
208: }
209:
210: $request = $method . ' ';
211: $request .= $urlInfo['path'] . '?' . $urlInfo['query'] . ' HTTP/1.1' . "\r\n";
212: $request .= $this->header . "\r\n";
213: $request .= $this->body;
214:
215: fwrite($handle, $request);
216:
217: $ret = '';
218: while (!feof($handle)) {
219: $ret .= fgets($handle);
220: }
221:
222: fclose($handle);
223:
224: if ($return) {
225: if (!$returnHeaders) {
226: $ret = substr(cString::strstr($ret, "\r\n\r\n"), strlen("\r\n\r\n"));
227: }
228: return $ret;
229: } else {
230: return strpos(cString::strstr($ret, '\r\n', true), '200') !== false;
231: }
232: }
233:
234: 235: 236:
237: public function postRequest($return = true, $returnHeaders = false) {
238: $this->preparePostRequest();
239:
240: return $this->sendRequest($return, 'POST', $returnHeaders);
241: }
242:
243: 244: 245:
246: public function getRequest($return = true, $returnHeaders = false) {
247: $this->prepareGetRequest();
248:
249: return $this->sendRequest($return, 'GET', $returnHeaders);
250: }
251:
252: 253: 254:
255: public function request($return = true, $returnHeaders = false) {
256: $this->prepareGetRequest();
257: $this->preparePostRequest();
258:
259: return $this->sendRequest($return, 'POST', $returnHeaders);
260: }
261: }
262:
263: ?>
264: