1: <?php
2: declare(strict_types=1);
3:
4: 5: 6: 7: 8: 9: 10: 11:
12: class CurlService
13: {
14: const DBG = false;
15:
16: public $error;
17:
18: 19: 20: 21: 22: 23: 24: 25:
26: public function post(string $url, array $data, array $header = [])
27: {
28:
29: $options = [
30: CURLOPT_POST => true,
31: CURLOPT_RETURNTRANSFER => true,
32: CURLOPT_URL => $url,
33: ];
34:
35:
36: $options[CURLINFO_HEADER_OUT] = true;
37: $options[CURLOPT_VERBOSE] = true;
38: $options[CURLOPT_FILETIME] = true;
39: $options[CURLOPT_FOLLOWLOCATION] = true;
40: $options[CURLOPT_MAXREDIRS] = 3;
41: $options[CURLOPT_SSL_VERIFYHOST] = false;
42: $options[CURLOPT_SSL_VERIFYPEER] = false;
43:
44: if (self::DBG) {
45: $additional = [
46: CURLOPT_HEADER => true,
47: CURLINFO_HEADER_OUT => true,
48: CURLOPT_VERBOSE => true,
49: CURLOPT_FILETIME => true,
50: CURLOPT_FOLLOWLOCATION => true,
51: CURLOPT_MAXREDIRS => 3,
52: CURLOPT_SSL_VERIFYHOST => false,
53: CURLOPT_SSL_VERIFYPEER => false,
54: ];
55: $options = array_merge($options, $additional);
56: }
57:
58:
59: if (!empty($data)) {
60: $options[CURLOPT_POSTFIELDS] = json_encode($data);
61: }
62:
63:
64: if (!empty($header)) {
65: $options[CURLOPT_HTTPHEADER] = $header;
66: }
67:
68: $response = json_decode($this->_call($options));
69:
70: return $response;
71: }
72:
73: 74: 75: 76: 77: 78: 79:
80: public function get(string $url, array $header = [])
81: {
82:
83: $options = [
84: CURLOPT_POST => false,
85: CURLOPT_RETURNTRANSFER => true,
86: CURLOPT_URL => $url,
87: ];
88:
89:
90: $options[CURLINFO_HEADER_OUT] = true;
91: $options[CURLOPT_VERBOSE] = true;
92: $options[CURLOPT_FILETIME] = true;
93: $options[CURLOPT_FOLLOWLOCATION] = true;
94: $options[CURLOPT_MAXREDIRS] = 3;
95: $options[CURLOPT_SSL_VERIFYHOST] = false;
96: $options[CURLOPT_SSL_VERIFYPEER] = false;
97:
98: if (self::DBG) {
99: $additional = [
100: CURLOPT_HEADER => true,
101: CURLINFO_HEADER_OUT => true,
102: CURLOPT_VERBOSE => true,
103: CURLOPT_FILETIME => true,
104: CURLOPT_FOLLOWLOCATION => true,
105: CURLOPT_MAXREDIRS => 3,
106: CURLOPT_SSL_VERIFYHOST => false,
107: CURLOPT_SSL_VERIFYPEER => false,
108: ];
109: $options = array_merge($options, $additional);
110: }
111:
112:
113: if (!empty($header)) {
114: $options[CURLOPT_HTTPHEADER] = $header;
115: }
116:
117: $response = $this->_call($options);
118:
119: return $response;
120: }
121:
122: 123: 124: 125: 126: 127:
128: private function _call(array $options)
129: {
130:
131: $curl = curl_init();
132: if (false === $curl) {
133: throw new CurlException('failed to init curl');
134: }
135:
136:
137: $succ = curl_setopt_array($curl, $options);
138: if (false === $succ) {
139: throw new CurlException('failed to set curl options');
140: }
141:
142:
143: $resp = curl_exec($curl);
144:
145: $info = curl_getinfo($curl);
146:
147:
148: curl_close($curl);
149:
150:
151: $this->error = [
152: 'resp' => $resp,
153: 'info' => $info,
154: ];
155: if (self::DBG) {
156: error_log($info);
157: }
158:
159: return $resp;
160: }
161: }
162:
163: class CurlException extends cException
164: {
165: }