From 97e8e3c8bd09453432699d988e599a1cc44c744b Mon Sep 17 00:00:00 2001 From: nguyenanhung Date: Mon, 24 Jul 2023 10:27:43 +0700 Subject: [PATCH] Fixed class Request --- helpers/request_helper.php | 22 ++++++++++++++++------ src/BaseHelper.php | 2 +- src/SimpleRestful.php | 20 ++++++++++++++++---- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/helpers/request_helper.php b/helpers/request_helper.php index 91fcddd..6492ed8 100644 --- a/helpers/request_helper.php +++ b/helpers/request_helper.php @@ -22,25 +22,35 @@ */ function sendSimpleGetRequest($url = '', $data = array(), $method = 'GET') { + $method = strtoupper($method); if ((!empty($data) && (is_array($data) || is_object($data)))) { $target = $url . '?' . http_build_query($data); } else { $target = $url; } - $defaultUA = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15'; - $method = strtoupper($method); + + $parseUrl = parse_url($target); + $UA = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15'; $curl = curl_init(); - curl_setopt_array($curl, array( + + $options = array( CURLOPT_URL => $target, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, CURLOPT_CUSTOMREQUEST => "GET", - CURLOPT_HTTPHEADER => array($defaultUA), - )); + CURLOPT_HTTPHEADER => array($UA), + ); + if (isset($parseUrl['scheme']) && $parseUrl['scheme'] === 'https') { + $options[CURLOPT_SSLVERSION] = CURL_SSLVERSION_TLSv1_2; + } + if (isset($parseUrl['scheme']) && $parseUrl['scheme'] === 'http') { + $options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; + } + + curl_setopt_array($curl, $options); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); diff --git a/src/BaseHelper.php b/src/BaseHelper.php index 5196116..a985723 100644 --- a/src/BaseHelper.php +++ b/src/BaseHelper.php @@ -19,7 +19,7 @@ */ class BaseHelper { - const VERSION = '1.4.3'; + const VERSION = '1.4.4'; const LAST_MODIFIED = '2023-07-24'; const PROJECT_NAME = 'CodeIgniter - Basic Helper'; const AUTHOR_NAME = 'Hung Nguyen'; diff --git a/src/SimpleRestful.php b/src/SimpleRestful.php index d5150ab..9025658 100644 --- a/src/SimpleRestful.php +++ b/src/SimpleRestful.php @@ -40,19 +40,31 @@ public static function execute($url, $type, $data = "", $header = null) $header = array("Content-Type: application/json"); } - curl_setopt_array($curl, array( - CURLOPT_URL => rtrim($url, "/"), + $url = rtrim($url, "/"); + $parseUrl = parse_url($url); + + $options = array( + CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => $type, CURLOPT_POSTFIELDS => $data, CURLOPT_HTTPHEADER => $header, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0, - )); + ); + + if (isset($parseUrl['scheme']) && $parseUrl['scheme'] === 'https') { + $options[CURLOPT_SSLVERSION] = CURL_SSLVERSION_TLSv1_2; + } + + if (isset($parseUrl['scheme']) && $parseUrl['scheme'] === 'http') { + $options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; + } + + curl_setopt_array($curl, $options); $response = json_decode(curl_exec($curl));