PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.10.2
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.10.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.10.2, at back/Helpers/global/curl.php

144 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 return [
79 'error' => curl_error($ch),
80 'status_code' => $httpCode,
81 ];
82 }
83
84 $result = json_decode($result, true);
85 $result['status_code'] = $httpCode;
86
87 return $result;
88 }
89
90 function acym_asyncUrlCalls(array $urls): void
91 {
92 if (!function_exists('curl_multi_exec')) {
93 return;
94 }
95
96 try {
97 $mh = curl_multi_init();
98
99 $handles = [];
100 foreach ($urls as $url) {
101 $ch = curl_init();
102 curl_setopt($ch, CURLOPT_URL, $url);
103 curl_setopt($ch, CURLOPT_HEADER, 0);
104 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
105 curl_multi_add_handle($mh, $ch);
106 $handles[] = $ch;
107 }
108
109 $running = null;
110 $time = 1;
111 do {
112 curl_multi_exec($mh, $running);
113 usleep(100);
114 if ($time > 50000) {
115 break;
116 }
117 $time++;
118 } while ($running);
119
120 foreach ($handles as $handle) {
121 curl_multi_remove_handle($mh, $handle);
122 }
123 curl_multi_close($mh);
124 } catch (Exception $e) {
125 $config = acym_config();
126 $reportPath = $config->get('cron_savepath');
127 if (!empty($reportPath)) {
128 $reportPath = str_replace(['{year}', '{month}'], [date('Y'), date('m')], $reportPath);
129 $reportPath = acym_cleanPath(ACYM_ROOT.trim(html_entity_decode($reportPath)));
130 acym_createDir(dirname($reportPath), true, true);
131
132 $lr = "\r\n";
133 file_put_contents(
134 $reportPath,
135 $lr.$lr.'******************** '.acym_getDate(
136 time()
137 ).' ********************'.$lr.'An error occurred while calling the queue sending script, please make sure the PHP function "curl_multi_exec" is activated on your server: '.$e->getMessage(
138 ),
139 FILE_APPEND
140 );
141 }
142 }
143 }
144