Skip to content

Commit 4fcc36e

Browse files
authored
Merge pull request #48 from nguyenanhung/develop
Develop
2 parents a2ed4b2 + 4d2fb9d commit 4fcc36e

File tree

3 files changed

+63
-58
lines changed

3 files changed

+63
-58
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,4 @@ bh_unicode_properties.cache
115115
GitHub.sublime-settings
116116

117117
test.php
118+
tmp/

src/BaseHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
*/
2020
class BaseHelper
2121
{
22-
const VERSION = '1.5.9.3';
23-
const LAST_MODIFIED = '2023-11-24';
22+
const VERSION = '1.6.0';
23+
const LAST_MODIFIED = '2024-01-15';
2424
const PROJECT_NAME = 'CodeIgniter - Basic Helper';
2525
const AUTHOR_NAME = 'Hung Nguyen';
2626
const AUTHOR_FULL_NAME = 'Hung Nguyen';

src/SimpleRequests.php

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,54 @@
1616
use Monolog\Formatter\LineFormatter;
1717

1818
/**
19-
* Class SimpleRequests
19+
* SimpleRequests Class
20+
*
21+
* This class provides basic functionalities for sending HTTP requests.
22+
* Refactored for better structure, readability, and PHP > 5.6 compatibility.
2023
*
2124
* @package nguyenanhung\CodeIgniter\BasicHelper
2225
* @author 713uk13m <dev@nguyenanhung.com>
2326
* @copyright 713uk13m <dev@nguyenanhung.com>
2427
*/
2528
class SimpleRequests
2629
{
27-
protected $mono;
28-
protected $DEBUG = false;
30+
protected $DEBUG = false;
31+
protected $logger = null;
2932
protected $logger_path = null;
30-
protected $logger_file;
31-
protected $timeout = 60;
32-
protected $header = array();
33+
protected $timeout = 60;
34+
protected $header = array();
3335

3436
/**
35-
* Requests constructor.
37+
* SimpleRequests constructor.
3638
*
37-
* @author : 713uk13m <dev@nguyenanhung.com>
39+
* @param $options
3840
* @copyright: 713uk13m <dev@nguyenanhung.com>
41+
* @author : 713uk13m <dev@nguyenanhung.com>
3942
*/
40-
public function __construct()
43+
public function __construct($options = [])
4144
{
42-
$this->logger_file = 'Log-' . date('Y-m-d') . '.log';
43-
$this->mono = array(
44-
'dateFormat' => "Y-m-d H:i:s u",
45-
'outputFormat' => "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
46-
'monoBubble' => true,
45+
$monolog = array(
46+
'dateFormat' => "Y-m-d H:i:s u",
47+
'outputFormat' => "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
48+
'monoBubble' => true,
4749
'monoFilePermission' => 0777
4850
);
51+
52+
if (isset($options['logger_path'])) {
53+
$this->logger_path = $options['logger_path'];
54+
}
55+
56+
if (isset($options['debug_status'])) {
57+
$this->DEBUG = $options['debug_status'];
58+
}
59+
60+
// create a log channel
61+
$formatter = new LineFormatter($monolog['outputFormat'], $monolog['dateFormat']);
62+
$stream = new StreamHandler($this->logger_path . 'Simple-Requests/Log-' . date('Y-m-d') . '.log', Logger::INFO, $monolog['monoBubble'], $monolog['monoFilePermission']);
63+
$stream->setFormatter($formatter);
64+
$this->logger = new Logger('SimpleRequests');
65+
$this->logger->pushHandler($stream);
66+
4967
}
5068

5169
/**
@@ -61,7 +79,6 @@ public function __construct()
6179
public function setTimeout($timeout)
6280
{
6381
$this->timeout = $timeout;
64-
6582
return $this;
6683
}
6784

@@ -78,15 +95,14 @@ public function setTimeout($timeout)
7895
public function setHeader($header = array())
7996
{
8097
$this->header = $header;
81-
8298
return $this;
8399
}
84100

85101
/**
86102
* Function sendRequest
87103
*
88104
* @param string $url
89-
* @param array $data
105+
* @param array $data
90106
* @param string $method
91107
*
92108
* @return string|null
@@ -98,79 +114,73 @@ public function sendRequest($url = '', $data = array(), $method = 'GET')
98114
{
99115
try {
100116
$getMethod = mb_strtoupper($method);
101-
// create a log channel
102-
$formatter = new LineFormatter($this->mono['outputFormat'], $this->mono['dateFormat']);
103-
$stream = new StreamHandler($this->logger_path . 'sendRequest/' . $this->logger_file, Logger::INFO, $this->mono['monoBubble'], $this->mono['monoFilePermission']);
104-
$stream->setFormatter($formatter);
105-
$logger = new Logger('Curl');
106-
$logger->pushHandler($stream);
107117
if ($this->DEBUG === true) {
108-
$logger->info('||=========== Logger Requests ===========||');
109-
$logger->info('Method: ' . $getMethod);
110-
$logger->info('Request: ' . $url, $data);
118+
$this->logger->info('||=========== Logger Send Requests ===========||');
119+
$this->logger->info('Send ' . $getMethod . ' Request to URL: ' . $url, $data);
111120
}
112-
// Curl
121+
113122
$curl = new BasicCurl();
114123
$curl->setOpt(CURLOPT_RETURNTRANSFER, true);
115124
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
116125
$curl->setOpt(CURLOPT_ENCODING, "utf-8");
117126
$curl->setOpt(CURLOPT_MAXREDIRS, 10);
118127
$curl->setOpt(CURLOPT_TIMEOUT, 300);
119-
// Request
128+
120129
if ('POST' === $getMethod) {
121130
$curl->post($url, $data);
122131
} else {
123132
$curl->get($url, $data);
124133
}
125-
// Response
134+
135+
126136
if ($curl->error) {
127137
$response = "cURL Error: " . $curl->error_message;
128138
} else {
129139
$response = $curl->response;
130140
}
131-
// Close Request
141+
142+
132143
$curl->close();
133-
// Log Response
144+
145+
134146
if ($this->DEBUG === true) {
135147
if (is_array($response) || is_object($response)) {
136-
$logger->info('Response: ' . json_encode($response));
148+
$this->logger->info('Response: ' . json_encode($response));
137149
} else {
138-
$logger->info('Response: ' . $response);
150+
$this->logger->info('Response: ' . $response);
139151
}
152+
140153
if (isset($curl->request_headers)) {
141154
if (is_array($curl->request_headers)) {
142-
$logger->info('Request Header: ', $curl->request_headers);
155+
$this->logger->info('Request Header: ', $curl->request_headers);
143156
} else {
144-
$logger->info('Request Header: ' . json_encode($curl->request_headers));
157+
$this->logger->info('Request Header: ' . json_encode($curl->request_headers));
145158
}
146159
}
160+
147161
if (isset($curl->response_headers)) {
148162
if (is_array($curl->response_headers)) {
149-
$logger->info('Response Header: ', $curl->response_headers);
163+
$this->logger->info('Response Header: ', $curl->response_headers);
150164
} else {
151-
$logger->info('Response Header: ' . json_encode($curl->response_headers));
165+
$this->logger->info('Response Header: ' . json_encode($curl->response_headers));
152166
}
153167
}
154168
}
155169

156-
// Return Response
157170
return $response;
158171
} catch (Exception $e) {
159172
log_message('error', __get_error_message__($e));
160173
log_message('error', __get_error_trace__($e));
161-
162174
return null;
163175
}
164176
}
165177

166-
// ========================================================================== //
167-
168178
/**
169179
* Function xmlRequest
170180
*
171181
* @param string $url
172182
* @param string $data
173-
* @param int $timeout
183+
* @param int $timeout
174184
*
175185
* @return bool|string|null
176186
* @author : 713uk13m <dev@nguyenanhung.com>
@@ -182,18 +192,13 @@ public function xmlRequest($url = '', $data = '', $timeout = 60)
182192
if (empty($url) || empty($data)) {
183193
return null;
184194
}
195+
185196
try {
186-
// create a log channel
187-
$formatter = new LineFormatter($this->mono['outputFormat'], $this->mono['dateFormat']);
188-
$stream = new StreamHandler($this->logger_path . 'xmlRequest/' . $this->logger_file, Logger::INFO, $this->mono['monoBubble'], $this->mono['monoFilePermission']);
189-
$stream->setFormatter($formatter);
190-
$logger = new Logger('request');
191-
$logger->pushHandler($stream);
192197
if ($this->DEBUG === true) {
193-
$logger->info('||=========== Logger xmlRequest ===========||');
194-
$logger->info('Request URL: ' . $url);
195-
$logger->info('Request Data: ' . $data);
198+
$this->logger->info('||=========== Logger xmlRequest ===========||');
199+
$this->logger->info('Send POST XML Request to URL: ' . $url . ' with DATA: ' . $data);
196200
}
201+
197202
$ch = curl_init();
198203
curl_setopt($ch, CURLOPT_URL, $url);
199204
$head[] = "Content-type: text/xml;charset=utf-8";
@@ -204,17 +209,17 @@ public function xmlRequest($url = '', $data = '', $timeout = 60)
204209
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
205210
curl_setopt($ch, CURLOPT_POST, 1);
206211
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
207-
$page = curl_exec($ch);
212+
$response = curl_exec($ch);
208213
curl_close($ch);
214+
209215
if ($this->DEBUG === true) {
210-
$logger->info('Response from Request: ' . $page);
216+
$this->logger->info('Response from Request: ' . $response);
211217
}
212218

213-
return $page;
219+
return $response;
214220
} catch (Exception $e) {
215221
log_message('error', __get_error_message__($e));
216222
log_message('error', __get_error_trace__($e));
217-
218223
return null;
219224
}
220225
}
@@ -235,7 +240,6 @@ public function xmlGetValue($xml, $openTag, $closeTag)
235240
{
236241
$f = mb_strpos($xml, $openTag) + mb_strlen($openTag);
237242
$l = mb_strpos($xml, $closeTag);
238-
239243
return ($f <= $l) ? mb_substr($xml, $f, $l - $f) : "";
240244
}
241245
}

0 commit comments

Comments
 (0)