Skip to content

Commit 06056c2

Browse files
authored
Merge pull request #29 from nguyenanhung/develop
Fixed class Request
2 parents 3fc1957 + 97e8e3c commit 06056c2

3 files changed

Lines changed: 33 additions & 11 deletions

File tree

helpers/request_helper.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,35 @@
2222
*/
2323
function sendSimpleGetRequest($url = '', $data = array(), $method = 'GET')
2424
{
25+
$method = strtoupper($method);
2526
if ((!empty($data) && (is_array($data) || is_object($data)))) {
2627
$target = $url . '?' . http_build_query($data);
2728
} else {
2829
$target = $url;
2930
}
30-
$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';
31-
$method = strtoupper($method);
31+
32+
$parseUrl = parse_url($target);
33+
$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';
3234
$curl = curl_init();
33-
curl_setopt_array($curl, array(
35+
36+
$options = array(
3437
CURLOPT_URL => $target,
3538
CURLOPT_RETURNTRANSFER => true,
3639
CURLOPT_ENCODING => "",
3740
CURLOPT_MAXREDIRS => 10,
3841
CURLOPT_TIMEOUT => 30,
3942
CURLOPT_FOLLOWLOCATION => true,
40-
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2,
4143
CURLOPT_CUSTOMREQUEST => "GET",
42-
CURLOPT_HTTPHEADER => array($defaultUA),
43-
));
44+
CURLOPT_HTTPHEADER => array($UA),
45+
);
46+
if (isset($parseUrl['scheme']) && $parseUrl['scheme'] === 'https') {
47+
$options[CURLOPT_SSLVERSION] = CURL_SSLVERSION_TLSv1_2;
48+
}
49+
if (isset($parseUrl['scheme']) && $parseUrl['scheme'] === 'http') {
50+
$options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1;
51+
}
52+
53+
curl_setopt_array($curl, $options);
4454
$response = curl_exec($curl);
4555
$err = curl_error($curl);
4656
curl_close($curl);

src/BaseHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
class BaseHelper
2121
{
22-
const VERSION = '1.4.3';
22+
const VERSION = '1.4.4';
2323
const LAST_MODIFIED = '2023-07-24';
2424
const PROJECT_NAME = 'CodeIgniter - Basic Helper';
2525
const AUTHOR_NAME = 'Hung Nguyen';

src/SimpleRestful.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,31 @@ public static function execute($url, $type, $data = "", $header = null)
4040
$header = array("Content-Type: application/json");
4141
}
4242

43-
curl_setopt_array($curl, array(
44-
CURLOPT_URL => rtrim($url, "/"),
43+
$url = rtrim($url, "/");
44+
$parseUrl = parse_url($url);
45+
46+
$options = array(
47+
CURLOPT_URL => $url,
4548
CURLOPT_RETURNTRANSFER => true,
4649
CURLOPT_ENCODING => "",
4750
CURLOPT_MAXREDIRS => 10,
4851
CURLOPT_TIMEOUT => 30,
49-
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
5052
CURLOPT_CUSTOMREQUEST => $type,
5153
CURLOPT_POSTFIELDS => $data,
5254
CURLOPT_HTTPHEADER => $header,
5355
CURLOPT_SSL_VERIFYHOST => 0,
5456
CURLOPT_SSL_VERIFYPEER => 0,
55-
));
57+
);
58+
59+
if (isset($parseUrl['scheme']) && $parseUrl['scheme'] === 'https') {
60+
$options[CURLOPT_SSLVERSION] = CURL_SSLVERSION_TLSv1_2;
61+
}
62+
63+
if (isset($parseUrl['scheme']) && $parseUrl['scheme'] === 'http') {
64+
$options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1;
65+
}
66+
67+
curl_setopt_array($curl, $options);
5668

5769
$response = json_decode(curl_exec($curl));
5870

0 commit comments

Comments
 (0)