Skip to content

Commit dfe76f3

Browse files
authored
Merge pull request #63 from nguyenanhung/v3.x
Optimize with mbstring
2 parents 9636633 + bdb2807 commit dfe76f3

File tree

9 files changed

+196
-197
lines changed

9 files changed

+196
-197
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"ext-json": "*",
2727
"ext-iconv": "*",
2828
"symfony/http-foundation": "^6.0 || ^5.3 || ^4.4 || ^3.4",
29+
"symfony/polyfill-mbstring": ">= 1.0",
2930
"php-curl-class/php-curl-class": "^9 || ^8",
3031
"guzzlehttp/guzzle": "^7 || ^6",
3132
"nguyenanhung/nusoap": "^0.9",

helpers/helpers.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
/**
1212
* Function sendSimpleRequest
1313
*
14-
* @param string $url URL Target Endpoint
15-
* @param string|array|object $data Array Data to Request
16-
* @param string $method GET or POST
14+
* @param string $url URL Target Endpoint
15+
* @param string|array|object $data Array Data to Request
16+
* @param string $method GET or POST
1717
*
1818
* @return bool|string|null
1919
* @author : 713uk13m <[email protected]>
@@ -23,20 +23,20 @@
2323
function sendSimpleRequest(string $url = '', $data = [], string $method = 'GET')
2424
{
2525
$target = (!empty($data) && (is_array($data) || is_object($data))) ? $url . '?' . http_build_query($data) : $url;
26-
$method = strtoupper($method);
27-
$curl = curl_init();
26+
$method = mb_strtoupper($method);
27+
$curl = curl_init();
2828
curl_setopt_array($curl, array(
29-
CURLOPT_URL => $target,
29+
CURLOPT_URL => $target,
3030
CURLOPT_RETURNTRANSFER => true,
31-
CURLOPT_ENCODING => "",
32-
CURLOPT_MAXREDIRS => 10,
33-
CURLOPT_TIMEOUT => 30,
34-
CURLOPT_CUSTOMREQUEST => $method,
35-
CURLOPT_POSTFIELDS => "",
36-
CURLOPT_HTTPHEADER => array(),
31+
CURLOPT_ENCODING => "",
32+
CURLOPT_MAXREDIRS => 10,
33+
CURLOPT_TIMEOUT => 30,
34+
CURLOPT_CUSTOMREQUEST => $method,
35+
CURLOPT_POSTFIELDS => "",
36+
CURLOPT_HTTPHEADER => array(),
3737
));
3838
$response = curl_exec($curl);
39-
$err = curl_error($curl);
39+
$err = curl_error($curl);
4040
curl_close($curl);
4141
if ($err) {
4242
$message = "cURL Error #: " . $err;
@@ -64,7 +64,6 @@ function sendSimpleRequest(string $url = '', $data = [], string $method = 'GET')
6464
function getIpAddress(bool $convertToInteger = false)
6565
{
6666
$ip = new nguyenanhung\MyRequests\Ip();
67-
6867
return $ip->getIpAddress($convertToInteger);
6968
}
7069
}

src/BackgroundRequest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct()
5555
{
5656
if (self::USE_BENCHMARK === true) {
5757
$this->benchmark = new Benchmark();
58-
$this->benchmark->mark('code_start');
58+
$this->benchmark->mark('code_background_request_start');
5959
}
6060
$this->logger = new Logger();
6161
if (empty($this->debugLoggerPath)) {
@@ -77,8 +77,8 @@ public function __construct()
7777
public function __destruct()
7878
{
7979
if (self::USE_BENCHMARK === true) {
80-
$this->benchmark->mark('code_end');
81-
$this->logger->debug(__FUNCTION__, 'Elapsed Time: ===> ' . $this->benchmark->elapsed_time('code_start', 'code_end'));
80+
$this->benchmark->mark('code_background_request_end');
81+
$this->logger->debug(__FUNCTION__, 'Elapsed Time: ===> ' . $this->benchmark->elapsed_time('code_background_request_start', 'code_background_request_end'));
8282
$this->logger->debug(__FUNCTION__, 'Memory Usage: ===> ' . $this->benchmark->memory_usage());
8383
}
8484
}
@@ -97,7 +97,7 @@ public function __destruct()
9797
public static function backgroundHttpGet(string $url): bool
9898
{
9999
$parts = parse_url($url);
100-
if (strtolower($parts['scheme']) === 'https') {
100+
if (mb_strtolower($parts['scheme']) === 'https') {
101101
$fp = fsockopen('ssl://' . $parts['host'], $parts['port'] ?? self::PORT_SSL, $errno, $errStr, self::REQUEST_TIMEOUT);
102102
} else {
103103
$fp = fsockopen($parts['host'], $parts['port'] ?? self::PORT_HTTP, $errno, $errStr, self::REQUEST_TIMEOUT);
@@ -123,7 +123,7 @@ public static function backgroundHttpGet(string $url): bool
123123
/**
124124
* Hàm gọi 1 async POST Request để không delay Main Process
125125
*
126-
* @param string $url Url Endpoint
126+
* @param string $url Url Endpoint
127127
* @param string $paramString Params to Request
128128
*
129129
* @return bool TRUE nếu thành công, FALSE nếu thất bại
@@ -151,7 +151,7 @@ public static function backgroundHttpPost(string $url, string $paramString = '')
151151
$out = "POST " . $parts['path'] . "?" . $parts['query'] . " HTTP/1.1\r\n";
152152
$out .= "Host: " . $parts['host'] . "\r\n";
153153
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
154-
$out .= "Content-Length: " . strlen($paramString) . "\r\n";
154+
$out .= "Content-Length: " . mb_strlen($paramString) . "\r\n";
155155
$out .= "Connection: Close\r\n\r\n";
156156
if ($paramString !== '') {
157157
$out .= $paramString;

src/CurlData.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,17 @@ public function createCurl(string $url = ''): self
651651
$this->error_code = $this->error ? ($this->curl_error ? $this->curl_error_code : $this->http_status_code) : 0;
652652
$this->request_headers = preg_split('/\r\n/', curl_getinfo($curl, CURLINFO_HEADER_OUT), null, PREG_SPLIT_NO_EMPTY);
653653
if (isset($this->response_headers['0'])) {
654-
$this->http_error_message = $this->error ? ($this->response_headers['0']) : '';
654+
if ($this->error) {
655+
$this->http_error_message = ($this->response_headers['0']);
656+
} else {
657+
$this->http_error_message = '';
658+
}
655659
} else {
656-
$this->http_error_message = $this->error ? ('') : '';
660+
if ($this->error) {
661+
$this->http_error_message = ('');
662+
} else {
663+
$this->http_error_message = '';
664+
}
657665
}
658666
$this->error_message = $this->curl_error ? $this->curl_error_message : $this->http_error_message;
659667
curl_close($curl);

src/GetContents.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public function useFileGetContents(): array
311311
}
312312

313313
if (isset($return['headers']['response_code'])) {
314-
$responseType = substr($return['headers']['response_code'], 0, 1);
314+
$responseType = mb_substr($return['headers']['response_code'], 0, 1);
315315
if ($responseType !== '2') {
316316
$return['error'] = array(
317317
'code' => $return['headers']['response_code'],
@@ -444,10 +444,10 @@ public function setURL(string $url = ''): self
444444
{
445445
try {
446446
if ($url !== '') {
447-
if (strpos($url, 'https://') === 0) {
447+
if (mb_strpos($url, 'https://') === 0) {
448448
$this->isSSL = true;
449449
$this->logger->debug(__FUNCTION__, 'Set SSL: ' . $this->isSSL);
450-
} elseif (strpos($url, 'http://') === 0) {
450+
} elseif (mb_strpos($url, 'http://') === 0) {
451451
$this->isSSL = true;
452452
$this->logger->debug(__FUNCTION__, 'Set SSL: ' . $this->isSSL);
453453
}
@@ -482,7 +482,7 @@ public function setMethod(string $method = '')
482482
$this->logger->debug(__FUNCTION__, 'Set Default Method = GET if $method is does not exist');
483483
$method = 'GET';
484484
} else {
485-
$method = strtoupper($method);
485+
$method = mb_strtoupper($method);
486486
$validMethods = array('GET', 'HEAD', 'PUT', 'POST', 'DELETE');
487487
if (!in_array($method, $validMethods)) {
488488
$message = "Error: " . __CLASS__ . ": The requested method (" . $method . ") is not valid here";

src/Input.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ public function inputStream($index = null, $xss_clean = null)
144144
public function method(bool $upper = false): string
145145
{
146146
return ($upper)
147-
? strtoupper($this->server('REQUEST_METHOD', true))
148-
: strtolower($this->server('REQUEST_METHOD', true));
147+
? mb_strtoupper($this->server('REQUEST_METHOD', true))
148+
: mb_strtolower($this->server('REQUEST_METHOD', true));
149149
}
150150

151151
/**
@@ -339,7 +339,7 @@ public function requestHeaders(bool $xss_clean = false)
339339
foreach ($_SERVER as $key => $val) {
340340
if (sscanf($key, 'HTTP_%s', $header) === 1) {
341341
// take SOME_HEADER and turn it into Some-Header
342-
$header = str_replace('_', ' ', strtolower($header));
342+
$header = str_replace('_', ' ', mb_strtolower($header));
343343
$header = str_replace(' ', '-', ucwords($header));
344344
$this->headers[$header] = $val;
345345
}
@@ -365,10 +365,10 @@ public function getRequestHeader(string $index, bool $xss_clean = false)
365365
if (!isset($headers)) {
366366
empty($this->headers) && $this->requestHeaders();
367367
foreach ($this->headers as $key => $value) {
368-
$headers[strtolower($key)] = $value;
368+
$headers[mb_strtolower($key)] = $value;
369369
}
370370
}
371-
$index = strtolower($index);
371+
$index = mb_strtolower($index);
372372
if (!isset($headers[$index])) {
373373
return null;
374374
}
@@ -387,7 +387,7 @@ public function getRequestHeader(string $index, bool $xss_clean = false)
387387
*/
388388
public function isAjax(): bool
389389
{
390-
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
390+
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && mb_strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
391391
}
392392

393393
/**

0 commit comments

Comments
 (0)