-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSimpleRestful.php
86 lines (71 loc) · 2.2 KB
/
SimpleRestful.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* Project codeigniter-basic-helper
* Created by PhpStorm
* User: 713uk13m <dev@nguyenanhung.com>
* Copyright: 713uk13m <dev@nguyenanhung.com>
* Date: 28/02/2022
* Time: 13:22
*/
namespace nguyenanhung\CodeIgniter\BasicHelper;
/**
* Class SimpleRestful
*
* @package nguyenanhung\CodeIgniter\BasicHelper
* @author 713uk13m <dev@nguyenanhung.com>
* @copyright 713uk13m <dev@nguyenanhung.com>
*/
class SimpleRestful extends BaseHelper
{
/**
* Function execute
*
* @param string $url
* @param string $type
* @param string $data
* @param mixed $header
*
* @return array|int
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 28/02/2022 25:41
*/
public static function execute($url, $type, $data = "", $header = null)
{
$curl = curl_init();
if (empty($header)) {
$header = array("Content-Type: application/json");
}
$url = rtrim($url, "/");
$parseUrl = parse_url($url);
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
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));
unset($response->response_time);
$err = curl_error($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
return -1;
}
return array('code' => $httpCode, 'response' => $response);
}
}