PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.2.2
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.2.2
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Helpers / global / curl.php

curl.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.2.2, at back/Helpers/global/curl.php

150 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function acym_makeCurlCall(string $url, array $options = []): array
4 {
5 $options['verifySsl'] = $options['verifySsl'] ?? true;
6
7 $headers = [];
8 if (!empty($options['headers']) && is_array($options['headers'])) {
9 $headers = $options['headers'];
10 }
11
12 $data = [];
13 if (!empty($options['data']) && is_array($options['data'])) {
14 $data = $options['data'];
15 }
16
17 $allowedMethods = ['GET', 'POST'];
18 $method = 'GET';
19 if (!empty($options['method']) && in_array($options['method'], $allowedMethods)) {
20 $method = $options['method'];
21 }
22
23 $dataFormatted = '';
24 if (!empty($data)) {
25 $isHeaderContentTypeJson = !empty($headers['Content-Type']) && $headers['Content-Type'] === 'application/json';
26 if ($method === 'POST' && $isHeaderContentTypeJson) {
27 $dataFormatted = json_encode($data);
28 } else {
29 $dataFormatted = http_build_query($data);
30 }
31 }
32
33 if ($method === 'GET' && !empty($dataFormatted)) {
34 $url .= strpos($url, '?') === false ? '?'.$dataFormatted : '&'.$dataFormatted;
35 }
36
37 $ch = curl_init();
38
39 curl_setopt($ch, CURLOPT_URL, $url);
40 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
41 if ($method === 'POST') {
42 curl_setopt($ch, CURLOPT_POST, true);
43 if (!empty($data)) {
44 curl_setopt($ch, CURLOPT_POSTFIELDS, $dataFormatted);
45 }
46 }
47 if (!$options['verifySsl']) {
48 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
49 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
50 }
51
52 if (!empty($headers)) {
53 $headersFormatted = array_map(
54 function ($key, $value) {
55 return $key.': '.$value;
56 },
57 array_keys($headers),
58 $headers
59 );
60 curl_setopt($ch, CURLOPT_HTTPHEADER, $headersFormatted);
61 }
62
63 if (!empty($options['dns']) && is_string($options['dns'])) {
64 curl_setopt($ch, CURLOPT_DNS_SERVERS, $options['dns']);
65 }
66
67 if (!empty($options['proxy']) && is_array($options['proxy'])) {
68 curl_setopt($ch, CURLOPT_PROXY, $options['proxy']['host']);
69 if (!empty($options['proxy']['auth'])) {
70 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $options['proxy']['auth']);
71 }
72 }
73
74 $result = curl_exec($ch);
75 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
76
77 if (curl_errno($ch)) {
78 $error = curl_error($ch);
79
80 curl_close($ch);
81
82 return [
83 'error' => $error,
84 'status_code' => $httpCode,
85 ];
86 }
87
88 curl_close($ch);
89
90 $result = json_decode($result, true);
91 $result['status_code'] = $httpCode;
92
93 return $result;
94 }
95
96 function acym_asyncCurlCall(array $urls): void
97 {
98 if (!function_exists('curl_multi_exec')) {
99 return;
100 }
101
102 try {
103 $mh = curl_multi_init();
104
105 $handles = [];
106 foreach ($urls as $url) {
107 $ch = curl_init();
108 curl_setopt($ch, CURLOPT_URL, $url);
109 curl_setopt($ch, CURLOPT_HEADER, 0);
110 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
111 curl_multi_add_handle($mh, $ch);
112 $handles[] = $ch;
113 }
114
115 $running = null;
116 $time = 1;
117 do {
118 curl_multi_exec($mh, $running);
119 usleep(100);
120 if ($time > 50000) {
121 break;
122 }
123 $time++;
124 } while ($running);
125
126 foreach ($handles as $handle) {
127 curl_multi_remove_handle($mh, $handle);
128 }
129 curl_multi_close($mh);
130 } catch (Exception $exception) {
131 $config = acym_config();
132 $reportPath = $config->get('cron_savepath');
133 if (!empty($reportPath)) {
134 $reportPath = str_replace(['{year}', '{month}'], [date('Y'), date('m')], $reportPath);
135 $reportPath = acym_cleanPath(ACYM_ROOT.trim(html_entity_decode($reportPath)));
136 acym_createDir(dirname($reportPath), true, true);
137
138 $lr = "\r\n";
139 file_put_contents(
140 $reportPath,
141 $lr.$lr.'******************** '.acym_getDate(
142 time()
143 ).' ********************'.$lr.'An error occurred while launching the multiple cron system, please make sure the PHP function "curl_multi_exec" is activated on your server: '.$exception->getMessage(
144 ),
145 FILE_APPEND
146 );
147 }
148 }
149 }
150