Skip to content

Commit

Permalink
Merge pull request #29 from nguyenanhung/develop
Browse files Browse the repository at this point in the history
Fixed class Request
  • Loading branch information
nguyenanhung authored Jul 24, 2023
2 parents 3fc1957 + 97e8e3c commit 06056c2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
22 changes: 16 additions & 6 deletions helpers/request_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/BaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
20 changes: 16 additions & 4 deletions src/SimpleRestful.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit 06056c2

Please sign in to comment.