From bdb2807db6ffa62884340d00cd0d03cdcf52e5d4 Mon Sep 17 00:00:00 2001 From: nguyenanhung Date: Sun, 13 Aug 2023 01:10:46 +0700 Subject: [PATCH] Optimize with mbstring --- composer.json | 1 + helpers/helpers.php | 27 ++-- src/BackgroundRequest.php | 12 +- src/CurlData.php | 12 +- src/GetContents.php | 8 +- src/Input.php | 12 +- src/MyRequests.php | 299 ++++++++++++++++++-------------------- src/ProjectInterface.php | 6 +- src/Utils.php | 16 +- 9 files changed, 196 insertions(+), 197 deletions(-) diff --git a/composer.json b/composer.json index 219f49e..eb88b10 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/helpers/helpers.php b/helpers/helpers.php index 18f78dc..2f5b0fd 100644 --- a/helpers/helpers.php +++ b/helpers/helpers.php @@ -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 @@ -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; @@ -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); } } diff --git a/src/BackgroundRequest.php b/src/BackgroundRequest.php index fb1f077..e997201 100644 --- a/src/BackgroundRequest.php +++ b/src/BackgroundRequest.php @@ -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)) { @@ -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()); } } @@ -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); @@ -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 @@ -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; diff --git a/src/CurlData.php b/src/CurlData.php index 63a6512..238358e 100644 --- a/src/CurlData.php +++ b/src/CurlData.php @@ -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); diff --git a/src/GetContents.php b/src/GetContents.php index 929f9cc..e4aca0b 100644 --- a/src/GetContents.php +++ b/src/GetContents.php @@ -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'], @@ -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); } @@ -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"; diff --git a/src/Input.php b/src/Input.php index 21cbd07..b02508d 100644 --- a/src/Input.php +++ b/src/Input.php @@ -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)); } /** @@ -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; } @@ -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; } @@ -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'); } /** diff --git a/src/MyRequests.php b/src/MyRequests.php index 61427dc..7ddb764 100644 --- a/src/MyRequests.php +++ b/src/MyRequests.php @@ -241,6 +241,70 @@ public function __destruct() } } + /** + * Function setDebugStatus + * + * @param bool $debugStatus + * + * @return MyRequests + * + * @author : 713uk13m + * @copyright: 713uk13m + */ + public function setDebugStatus(bool $debugStatus): self + { + $this->debugStatus = $debugStatus; + return $this; + } + + /** + * Function setDebugLevel + * + * @param string|null $debugLevel + * + * @return MyRequests + * + * @author : 713uk13m + * @copyright: 713uk13m + */ + public function setDebugLevel(string $debugLevel): self + { + $this->debugLevel = $debugLevel; + return $this; + } + + /** + * Function setDebugLoggerPath + * + * @param string|null $debugLoggerPath + * + * @return MyRequests + * + * @author : 713uk13m + * @copyright: 713uk13m + */ + public function setDebugLoggerPath(string $debugLoggerPath): self + { + $this->debugLoggerPath = $debugLoggerPath; + return $this; + } + + /** + * Function setDebugLoggerFilename + * + * @param string $debugLoggerFilename + * + * @return MyRequests + * + * @author : 713uk13m + * @copyright: 713uk13m + */ + public function setDebugLoggerFilename(string $debugLoggerFilename): self + { + $this->debugLoggerFilename = $debugLoggerFilename; + return $this; + } + /** * Function setHeader * @@ -394,9 +458,9 @@ public function setRequestIsXml(bool $isXml = false): self { $this->isXml = $isXml; if ($this->isXml === true) { - $header['Accept'] = 'text/xml; charset=utf-8'; + $header['Accept'] = 'text/xml; charset=utf-8'; $header['Content-type'] = 'text/xml; charset=utf-8'; - $this->headers = $header; + $this->headers = $header; } $this->logger->info(__FUNCTION__, 'setRequestIsXml: ' . $this->isXml); @@ -417,9 +481,9 @@ public function setRequestIsJson(bool $isJson = false): self { $this->isJson = $isJson; if ($this->isJson === true) { - $header['Accept'] = 'application/json; charset=utf-8'; + $header['Accept'] = 'application/json; charset=utf-8'; $header['Content-type'] = 'application/json; charset=utf-8'; - $this->headers = $header; + $this->headers = $header; } $this->logger->info(__FUNCTION__, 'setRequestIsJson: ' . $this->isJson); @@ -610,8 +674,8 @@ public function getResponseHeader() * Function guzzlePhpRequest * Send Request use GuzzleHttp\Client - https://packagist.org/packages/guzzlehttp/guzzle * - * @param string $url URL Endpoint to be Request - * @param array $data Data Content to be Request + * @param string $url URL Endpoint to be Request + * @param array $data Data Content to be Request * @param string $method Set Method to be Request * * @return array|\Psr\Http\Message\ResponseInterface|\Psr\Http\Message\StreamInterface|string|null @@ -627,7 +691,7 @@ public function guzzlePhpRequest(string $url = '', array $data = array(), string $this->logger->debug(__FUNCTION__, '/------------> ' . __FUNCTION__ . ' <------------\\'); $inputParams = array('url' => $url, 'data' => $data, 'method' => $method); $this->logger->debug(__FUNCTION__, 'input Params: ', $inputParams); - $method = strtoupper($method); + $method = mb_strtoupper($method); $endpoint = trim($url); $this->logger->debug(__FUNCTION__, 'cURL Endpoint: ' . $endpoint); if (PHP_VERSION_ID < 50400) { @@ -641,7 +705,7 @@ public function guzzlePhpRequest(string $url = '', array $data = array(), string $client = new Client(); // Create options $options = array( - 'timeout' => $this->timeout, + 'timeout' => $this->timeout, 'connect_timeout' => $this->timeout ); if (is_array($this->headers) && count($this->headers) > 0) { @@ -694,38 +758,38 @@ public function guzzlePhpRequest(string $url = '', array $data = array(), string $request = $client->get($endpoint, $this->options); } // Debug - $status_code = $request->getStatusCode(); - $status_message = $request->getReasonPhrase(); - $http_error = in_array(floor($this->http_code / 100), [4, 5], true); - $error_code = array( - 'status' => $status_code, - 'error' => $status_code, - 'error_code' => $status_code, + $status_code = $request->getStatusCode(); + $status_message = $request->getReasonPhrase(); + $http_error = in_array(floor($this->http_code / 100), [4, 5], true); + $error_code = array( + 'status' => $status_code, + 'error' => $status_code, + 'error_code' => $status_code, 'error_message' => $status_message, - 'http_error' => array( - 'http_error' => $http_error, - 'http_status_code' => $status_code, + 'http_error' => array( + 'http_error' => $http_error, + 'http_status_code' => $status_code, 'http_error_message' => $status_message ), - 'headers' => array( - 'request_headers' => $this->headers, + 'headers' => array( + 'request_headers' => $this->headers, 'response_headers' => $request->getHeaders() ), - 'data' => array( - 'status' => $request->getStatusCode(), - 'error_code' => $request->getStatusCode(), - 'error_message' => $request->getReasonPhrase(), - 'reasonPhrase' => $request->getReasonPhrase(), - 'protocolVersion' => $request->getProtocolVersion(), - 'headers' => $request->getHeaders(), - 'requests_url' => $endpoint, + 'data' => array( + 'status' => $request->getStatusCode(), + 'error_code' => $request->getStatusCode(), + 'error_message' => $request->getReasonPhrase(), + 'reasonPhrase' => $request->getReasonPhrase(), + 'protocolVersion' => $request->getProtocolVersion(), + 'headers' => $request->getHeaders(), + 'requests_url' => $endpoint, 'requests_options' => $this->options, - 'response_body' => $request->getBody() + 'response_body' => $request->getBody() ) ); - $this->http_code = $status_code; + $this->http_code = $status_code; $this->http_message = $request->getReasonPhrase(); - $this->error_code = $error_code; + $this->error_code = $error_code; $this->logger->debug(__FUNCTION__, 'Full Data Curl Message and Http Message: ', $error_code); if ($http_error) { if ($this->errorResponseIsData === true) { @@ -764,9 +828,9 @@ public function guzzlePhpRequest(string $url = '', array $data = array(), string * Function curlRequest * Send Request use \Curl\Curl class - https://packagist.org/packages/curl/curl * - * @param string $url URL Endpoint to be Request - * @param array|string $data Data Content to be Request - * @param string $method Set Method to be Request + * @param string $url URL Endpoint to be Request + * @param array|string $data Data Content to be Request + * @param string $method Set Method to be Request * * @return array|null|string Response content from server, * null of Exception Message if Error @@ -780,7 +844,7 @@ public function curlRequest(string $url = '', array $data = array(), string $met $this->logger->debug(__FUNCTION__, '/------------> ' . __FUNCTION__ . ' <------------\\'); $inputParams = array('url' => $url, 'data' => $data, 'method' => $method); $this->logger->info(__FUNCTION__, 'input Params: ', $inputParams); - $method = strtoupper($method); + $method = mb_strtoupper($method); if (!class_exists(Curl::class)) { $this->logger->critical(__FUNCTION__, 'class \Curl\Curl() is not exits'); $response = null; @@ -850,9 +914,9 @@ public function curlRequest(string $url = '', array $data = array(), string $met } $error_code = $this->curlRequestErrorParse($curl); // Set Vars - $this->error_code = $error_code; - $this->http_code = $error_code['http_error']['http_status_code']; - $this->http_message = $error_code['http_error']['http_error_message']; + $this->error_code = $error_code; + $this->http_code = $error_code['http_error']['http_status_code']; + $this->http_message = $error_code['http_error']['http_error_message']; $this->requests_header = $error_code['headers']['request_headers']; $this->response_header = $error_code['headers']['response_headers']; // Response @@ -866,7 +930,7 @@ public function curlRequest(string $url = '', array $data = array(), string $met $this->logger->debug(__FUNCTION__, 'Return Error Response is Null'); $response = null; } else { - $errMsg = $error_code['error_message']; + $errMsg = $error_code['error_message']; $response = "cURL Error: " . $errMsg; $this->logger->debug(__FUNCTION__, 'Return Error Response is Message: ' . $response); } @@ -984,22 +1048,22 @@ protected function curlRequestErrorParse($curl): array } return array( - 'status' => $resErrorCode, - 'error' => $curl->error, - 'error_code' => $resErrorCode, + 'status' => $resErrorCode, + 'error' => $curl->error, + 'error_code' => $resErrorCode, 'error_message' => $resErrorMessage, - 'curl_error' => array( - 'curl_error' => $resErrorCurl, - 'curl_error_code' => $resErrorCurlCode, + 'curl_error' => array( + 'curl_error' => $resErrorCurl, + 'curl_error_code' => $resErrorCurlCode, 'curl_error_message' => $resErrorCurlMessage ), - 'http_error' => array( - 'http_error' => $resErrorHttp, - 'http_status_code' => $resErrorHttpStatusCode, + 'http_error' => array( + 'http_error' => $resErrorHttp, + 'http_status_code' => $resErrorHttpStatusCode, 'http_error_message' => $resErrorHttpMessage ), - 'headers' => array( - 'request_headers' => $resRequestHeaders, + 'headers' => array( + 'request_headers' => $resRequestHeaders, 'response_headers' => $resResponseHeaders ) ); @@ -1009,9 +1073,9 @@ protected function curlRequestErrorParse($curl): array * Function sendRequest * Handle send Request use Multi Method * - * @param string $url URL Endpoint to be Request - * @param array|string $data Data Content to be Request - * @param string $method Set Method to be Request + * @param string $url URL Endpoint to be Request + * @param array|string $data Data Content to be Request + * @param string $method Set Method to be Request * * @return array|mixed|object|\Psr\Http\Message\ResponseInterface|\Psr\Http\Message\StreamInterface|string|null Response content from server * null of Exception Message if Error @@ -1024,14 +1088,14 @@ public function sendRequest(string $url = '', $data = array(), string $method = $this->logger->debug(__FUNCTION__, '/------------> ' . __FUNCTION__ . ' <------------\\'); $inputParams = array('url' => $url, 'data' => $data, 'method' => $method); $this->logger->debug(__FUNCTION__, 'input Params: ', $inputParams); - $method = strtoupper($method); + $method = mb_strtoupper($method); $endpoint = trim($url); $this->logger->debug(__FUNCTION__, 'cURL Endpoint: ' . $endpoint); if (!extension_loaded('curl')) { $this->logger->critical(__FUNCTION__, 'Server is not Support cURL, Please cURL. Library fallback user File Get Contents'); // Create Request use File Get Content - $content = new GetContents(); - $content->debugStatus = $this->debugStatus; + $content = new GetContents(); + $content->debugStatus = $this->debugStatus; $content->debugLoggerPath = $this->debugLoggerPath; $content->__construct(); $content->setURL($url); @@ -1040,9 +1104,9 @@ public function sendRequest(string $url = '', $data = array(), string $method = $content->setData($data); $content->sendRequest(); // Create Request - $result = $content->response(); + $result = $content->response(); $getContent = $content->getContent(); - $getError = $content->getError(); + $getError = $content->getError(); $this->logger->debug(__FUNCTION__, 'Get Content Result: ' . $getContent); $this->logger->debug(__FUNCTION__, 'Get Error Result: ' . $getError); } else { @@ -1076,9 +1140,9 @@ public function sendRequest(string $url = '', $data = array(), string $method = * Function xmlRequest * Send XML Request to Server * - * @param string $url URL Endpoint to be Request - * @param string $data Data Content to be Request - * @param int $timeout Timeout Request + * @param string $url URL Endpoint to be Request + * @param string $data Data Content to be Request + * @param int $timeout Timeout Request * * @return array|null|string Response from Server * @author: 713uk13m @@ -1094,10 +1158,9 @@ public function xmlRequest(string $url = '', string $data = '', int $timeout = 6 $this->logger->debug(__FUNCTION__, 'input Params: ', $inputParams); if (!extension_loaded('curl')) { $this->logger->critical(__FUNCTION__, 'Server is not Support cURL, Please cURL. Library fallback user File Get Contents'); - // Create Request use File Get Content - $content = new GetContents(); - $content->debugStatus = $this->debugStatus; + $content = new GetContents(); + $content->debugStatus = $this->debugStatus; $content->debugLoggerPath = $this->debugLoggerPath; $content->__construct(); $content->setURL($url); @@ -1105,11 +1168,10 @@ public function xmlRequest(string $url = '', string $data = '', int $timeout = 6 $content->setXML(true); $content->setData($data); $content->sendRequest(); - // Create Request - $result = $content->response(); + $result = $content->response(); $getContent = $content->getContent(); - $getError = $content->getError(); + $getError = $content->getError(); $this->logger->debug(__FUNCTION__, 'Get Content Result: ' . $getContent); $this->logger->debug(__FUNCTION__, 'Get Error Result: ' . $getError); } else { @@ -1123,7 +1185,6 @@ public function xmlRequest(string $url = '', string $data = '', int $timeout = 6 $this->logger->debug(__FUNCTION__, 'Final Result from Request: ' . trim($result)); } - return $result; } @@ -1131,9 +1192,9 @@ public function xmlRequest(string $url = '', string $data = '', int $timeout = 6 * Function jsonRequest * Send JSON Request to Server * - * @param string $url URL Endpoint to be Request - * @param array $data Data Content to be Request - * @param int $timeout Timeout Request + * @param string $url URL Endpoint to be Request + * @param array $data Data Content to be Request + * @param int $timeout Timeout Request * * @return array|null|string Response from Server * @author: 713uk13m @@ -1149,10 +1210,9 @@ public function jsonRequest(string $url = '', array $data = array(), int $timeou $this->logger->debug(__FUNCTION__, 'input Params: ', $inputParams); if (!extension_loaded('curl')) { $this->logger->critical(__FUNCTION__, 'Server is not Support cURL, Please cURL. Library fallback user File Get Contents'); - // Create Request use File Get Content - $content = new GetContents(); - $content->debugStatus = $this->debugStatus; + $content = new GetContents(); + $content->debugStatus = $this->debugStatus; $content->debugLoggerPath = $this->debugLoggerPath; $content->__construct(); $content->setURL($url); @@ -1160,11 +1220,10 @@ public function jsonRequest(string $url = '', array $data = array(), int $timeou $content->setJson(true); $content->setData($data); $content->sendRequest(); - // Create Request - $result = $content->response(); + $result = $content->response(); $getContent = $content->getContent(); - $getError = $content->getError(); + $getError = $content->getError(); $this->logger->debug(__FUNCTION__, 'Get Content Result: ' . $getContent); $this->logger->debug(__FUNCTION__, 'Get Error Result: ' . $getError); } else { @@ -1187,25 +1246,25 @@ public function jsonRequest(string $url = '', array $data = array(), int $timeou /** * Function xmlGetValue * - * @param string $xml XML String - * @param string $openTag OpenTag to find + * @param string $xml XML String + * @param string $openTag OpenTag to find * @param string $closeTag CloseTag to find * - * @return bool|string Result from Tag, Empty string if not + * @return string Result from Tag, Empty string if not * * @author : 713uk13m * @copyright : 713uk13m * @time : 10/7/18 06:57 */ - public function xmlGetValue(string $xml = '', string $openTag = '', string $closeTag = '') + public function xmlGetValue(string $xml = '', string $openTag = '', string $closeTag = ''): string { if (empty($xml) || empty($openTag) || empty($closeTag)) { return ''; } - $f = strpos($xml, $openTag) + strlen($openTag); - $l = strpos($xml, $closeTag); + $f = mb_strpos($xml, $openTag) + mb_strlen($openTag); + $l = mb_strpos($xml, $closeTag); - return ($f <= $l) ? substr($xml, $f, $l - $f) : ""; + return ($f <= $l) ? mb_substr($xml, $f, $l - $f) : ""; } /** @@ -1213,7 +1272,7 @@ public function xmlGetValue(string $xml = '', string $openTag = '', string $clos * * @param string $resultXml XML String to Parse * - * @return false|string + * @return string|false * * @author : 713uk13m * @copyright : 713uk13m @@ -1222,78 +1281,10 @@ public function xmlGetValue(string $xml = '', string $openTag = '', string $clos public function parseXmlDataRequest(string $resultXml = '') { $array = array( - 'ec' => $this->xmlGetValue($resultXml, "", ""), + 'ec' => $this->xmlGetValue($resultXml, "", ""), 'msg' => $this->xmlGetValue($resultXml, "", "") ); return json_encode($array); } - - /** - * Function setDebugStatus - * - * @param bool $debugStatus - * - * @return MyRequests - * - * @author : 713uk13m - * @copyright: 713uk13m - */ - public function setDebugStatus(bool $debugStatus): MyRequests - { - $this->debugStatus = $debugStatus; - - return $this; - } - - /** - * Function setDebugLevel - * - * @param string|null $debugLevel - * - * @return MyRequests - * - * @author : 713uk13m - * @copyright: 713uk13m - */ - public function setDebugLevel(string $debugLevel): MyRequests - { - $this->debugLevel = $debugLevel; - - return $this; - } - - /** - * Function setDebugLoggerPath - * - * @param string|null $debugLoggerPath - * - * @return MyRequests - * - * @author : 713uk13m - * @copyright: 713uk13m - */ - public function setDebugLoggerPath(string $debugLoggerPath): MyRequests - { - $this->debugLoggerPath = $debugLoggerPath; - - return $this; - } - - /** - * Function setDebugLoggerFilename - * - * @param string $debugLoggerFilename - * - * @return MyRequests - * - * @author : 713uk13m - * @copyright: 713uk13m - */ - public function setDebugLoggerFilename(string $debugLoggerFilename): MyRequests - { - $this->debugLoggerFilename = $debugLoggerFilename; - - return $this; - } } diff --git a/src/ProjectInterface.php b/src/ProjectInterface.php index 874f3d9..e8dd36d 100644 --- a/src/ProjectInterface.php +++ b/src/ProjectInterface.php @@ -18,8 +18,8 @@ */ interface ProjectInterface { - const VERSION = '3.0.8'; - const LAST_MODIFIED = '2023-02-14'; + const VERSION = '3.0.9'; + const LAST_MODIFIED = '2023-08-13'; const MIN_PHP_VERSION = '7.0'; const GET = 'GET'; const HEAD = 'HEAD'; @@ -38,10 +38,10 @@ interface ProjectInterface /** * Hàm lấy phiên bản hiện tại của Package * + * @return string Phiên bản hiện tại của Package, VD: 3.0.2 * @author: 713uk13m * @time : 10/7/18 01:05 * - * @return string Phiên bản hiện tại của Package, VD: 3.0.2 */ public function getVersion(): string; } diff --git a/src/Utils.php b/src/Utils.php index 4a8efe9..89a9602 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -117,7 +117,7 @@ public static function getBrowserLanguage() { $language = ''; if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - $language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); + $language = mb_substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); } return $language; @@ -135,7 +135,7 @@ public static function getBrowserLanguage() */ public static function paddingWebsitePrefix($url): string { - if (strpos($url, 'http') !== 0) { + if (mb_strpos($url, 'http') !== 0) { $url = 'http://' . $url; } @@ -156,7 +156,7 @@ public static function paddingWebsitePrefix($url): string public static function urlAddParam($url, $paramString): string { // neu chua co dau ? - if (strpos($url, '?') === false) { + if (mb_strpos($url, '?') === false) { $url .= '?'; } @@ -202,10 +202,10 @@ public static function refineDashInUrl($url) { $url = preg_replace('/[-]+/', '-', $url); if ($url[0] === '-') { - $url = substr($url, 1); + $url = mb_substr($url, 1); } - if ($url[strlen($url) - 1] === '-') { - $url = substr($url, 0, -1); + if ($url[mb_strlen($url) - 1] === '-') { + $url = mb_substr($url, 0, -1); } return $url; @@ -236,7 +236,7 @@ public static function saveExternalFile($img, $fullPath, $type = 'image', bool $ $raw_data = curl_exec($ch); curl_close($ch); //check if return error (include html in output) - if (strpos($raw_data, 'html') === false) { + if (mb_strpos($raw_data, 'html') === false) { $fp = fopen($fullPath, 'wb'); if (!$fp) { return false; @@ -253,7 +253,7 @@ public static function saveExternalFile($img, $fullPath, $type = 'image', bool $ return false; } $file_headers = @get_headers($img); - if (strpos($file_headers[0], '200') || strpos($file_headers[0], '302') || strpos($file_headers[0], '304')) { + if (mb_strpos($file_headers[0], '200') || mb_strpos($file_headers[0], '302') || mb_strpos($file_headers[0], '304')) { return file_put_contents($fullPath, file_get_contents($img)); }