Skip to content

Commit

Permalink
Merge pull request #63 from nguyenanhung/v3.x
Browse files Browse the repository at this point in the history
Optimize with mbstring
  • Loading branch information
nguyenanhung authored Aug 12, 2023
2 parents 9636633 + bdb2807 commit dfe76f3
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 197 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"ext-json": "*",
"ext-iconv": "*",
"symfony/http-foundation": "^6.0 || ^5.3 || ^4.4 || ^3.4",
"symfony/polyfill-mbstring": ">= 1.0",
"php-curl-class/php-curl-class": "^9 || ^8",
"guzzlehttp/guzzle": "^7 || ^6",
"nguyenanhung/nusoap": "^0.9",
Expand Down
27 changes: 13 additions & 14 deletions helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
/**
* Function sendSimpleRequest
*
* @param string $url URL Target Endpoint
* @param string|array|object $data Array Data to Request
* @param string $method GET or POST
* @param string $url URL Target Endpoint
* @param string|array|object $data Array Data to Request
* @param string $method GET or POST
*
* @return bool|string|null
* @author : 713uk13m <dev@nguyenanhung.com>
Expand All @@ -23,20 +23,20 @@
function sendSimpleRequest(string $url = '', $data = [], string $method = 'GET')
{
$target = (!empty($data) && (is_array($data) || is_object($data))) ? $url . '?' . http_build_query($data) : $url;
$method = strtoupper($method);
$curl = curl_init();
$method = mb_strtoupper($method);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $target,
CURLOPT_URL => $target,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(),
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$message = "cURL Error #: " . $err;
Expand Down Expand Up @@ -64,7 +64,6 @@ function sendSimpleRequest(string $url = '', $data = [], string $method = 'GET')
function getIpAddress(bool $convertToInteger = false)
{
$ip = new nguyenanhung\MyRequests\Ip();

return $ip->getIpAddress($convertToInteger);
}
}
12 changes: 6 additions & 6 deletions src/BackgroundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct()
{
if (self::USE_BENCHMARK === true) {
$this->benchmark = new Benchmark();
$this->benchmark->mark('code_start');
$this->benchmark->mark('code_background_request_start');
}
$this->logger = new Logger();
if (empty($this->debugLoggerPath)) {
Expand All @@ -77,8 +77,8 @@ public function __construct()
public function __destruct()
{
if (self::USE_BENCHMARK === true) {
$this->benchmark->mark('code_end');
$this->logger->debug(__FUNCTION__, 'Elapsed Time: ===> ' . $this->benchmark->elapsed_time('code_start', 'code_end'));
$this->benchmark->mark('code_background_request_end');
$this->logger->debug(__FUNCTION__, 'Elapsed Time: ===> ' . $this->benchmark->elapsed_time('code_background_request_start', 'code_background_request_end'));
$this->logger->debug(__FUNCTION__, 'Memory Usage: ===> ' . $this->benchmark->memory_usage());
}
}
Expand All @@ -97,7 +97,7 @@ public function __destruct()
public static function backgroundHttpGet(string $url): bool
{
$parts = parse_url($url);
if (strtolower($parts['scheme']) === 'https') {
if (mb_strtolower($parts['scheme']) === 'https') {
$fp = fsockopen('ssl://' . $parts['host'], $parts['port'] ?? self::PORT_SSL, $errno, $errStr, self::REQUEST_TIMEOUT);
} else {
$fp = fsockopen($parts['host'], $parts['port'] ?? self::PORT_HTTP, $errno, $errStr, self::REQUEST_TIMEOUT);
Expand All @@ -123,7 +123,7 @@ public static function backgroundHttpGet(string $url): bool
/**
* Hàm gọi 1 async POST Request để không delay Main Process
*
* @param string $url Url Endpoint
* @param string $url Url Endpoint
* @param string $paramString Params to Request
*
* @return bool TRUE nếu thành công, FALSE nếu thất bại
Expand Down Expand Up @@ -151,7 +151,7 @@ public static function backgroundHttpPost(string $url, string $paramString = '')
$out = "POST " . $parts['path'] . "?" . $parts['query'] . " HTTP/1.1\r\n";
$out .= "Host: " . $parts['host'] . "\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "Content-Length: " . strlen($paramString) . "\r\n";
$out .= "Content-Length: " . mb_strlen($paramString) . "\r\n";
$out .= "Connection: Close\r\n\r\n";
if ($paramString !== '') {
$out .= $paramString;
Expand Down
12 changes: 10 additions & 2 deletions src/CurlData.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,17 @@ public function createCurl(string $url = ''): self
$this->error_code = $this->error ? ($this->curl_error ? $this->curl_error_code : $this->http_status_code) : 0;
$this->request_headers = preg_split('/\r\n/', curl_getinfo($curl, CURLINFO_HEADER_OUT), null, PREG_SPLIT_NO_EMPTY);
if (isset($this->response_headers['0'])) {
$this->http_error_message = $this->error ? ($this->response_headers['0']) : '';
if ($this->error) {
$this->http_error_message = ($this->response_headers['0']);
} else {
$this->http_error_message = '';
}
} else {
$this->http_error_message = $this->error ? ('') : '';
if ($this->error) {
$this->http_error_message = ('');
} else {
$this->http_error_message = '';
}
}
$this->error_message = $this->curl_error ? $this->curl_error_message : $this->http_error_message;
curl_close($curl);
Expand Down
8 changes: 4 additions & 4 deletions src/GetContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function useFileGetContents(): array
}

if (isset($return['headers']['response_code'])) {
$responseType = substr($return['headers']['response_code'], 0, 1);
$responseType = mb_substr($return['headers']['response_code'], 0, 1);
if ($responseType !== '2') {
$return['error'] = array(
'code' => $return['headers']['response_code'],
Expand Down Expand Up @@ -444,10 +444,10 @@ public function setURL(string $url = ''): self
{
try {
if ($url !== '') {
if (strpos($url, 'https://') === 0) {
if (mb_strpos($url, 'https://') === 0) {
$this->isSSL = true;
$this->logger->debug(__FUNCTION__, 'Set SSL: ' . $this->isSSL);
} elseif (strpos($url, 'http://') === 0) {
} elseif (mb_strpos($url, 'http://') === 0) {
$this->isSSL = true;
$this->logger->debug(__FUNCTION__, 'Set SSL: ' . $this->isSSL);
}
Expand Down Expand Up @@ -482,7 +482,7 @@ public function setMethod(string $method = '')
$this->logger->debug(__FUNCTION__, 'Set Default Method = GET if $method is does not exist');
$method = 'GET';
} else {
$method = strtoupper($method);
$method = mb_strtoupper($method);
$validMethods = array('GET', 'HEAD', 'PUT', 'POST', 'DELETE');
if (!in_array($method, $validMethods)) {
$message = "Error: " . __CLASS__ . ": The requested method (" . $method . ") is not valid here";
Expand Down
12 changes: 6 additions & 6 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public function inputStream($index = null, $xss_clean = null)
public function method(bool $upper = false): string
{
return ($upper)
? strtoupper($this->server('REQUEST_METHOD', true))
: strtolower($this->server('REQUEST_METHOD', true));
? mb_strtoupper($this->server('REQUEST_METHOD', true))
: mb_strtolower($this->server('REQUEST_METHOD', true));
}

/**
Expand Down Expand Up @@ -339,7 +339,7 @@ public function requestHeaders(bool $xss_clean = false)
foreach ($_SERVER as $key => $val) {
if (sscanf($key, 'HTTP_%s', $header) === 1) {
// take SOME_HEADER and turn it into Some-Header
$header = str_replace('_', ' ', strtolower($header));
$header = str_replace('_', ' ', mb_strtolower($header));
$header = str_replace(' ', '-', ucwords($header));
$this->headers[$header] = $val;
}
Expand All @@ -365,10 +365,10 @@ public function getRequestHeader(string $index, bool $xss_clean = false)
if (!isset($headers)) {
empty($this->headers) && $this->requestHeaders();
foreach ($this->headers as $key => $value) {
$headers[strtolower($key)] = $value;
$headers[mb_strtolower($key)] = $value;
}
}
$index = strtolower($index);
$index = mb_strtolower($index);
if (!isset($headers[$index])) {
return null;
}
Expand All @@ -387,7 +387,7 @@ public function getRequestHeader(string $index, bool $xss_clean = false)
*/
public function isAjax(): bool
{
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && mb_strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
}

/**
Expand Down
Loading

0 comments on commit dfe76f3

Please sign in to comment.